[LINUX] Memorandum of sed

A memorandum for when you want to replace data in bulk using sed. Will be added at any time.

Delete

First, when you want to delete even and odd lines respectively. As a preparation, prepare a file that outputs different character strings on even and odd lines.

$ for i in {1..10}; do
if [ $(($i % 2)) = 0 ]; then
echo 'Even lines'
else
echo 'Odd line'
fi
done > number.txt

$ cat number.txt
Odd line
Even lines
Odd line
Even lines
Odd line
Even lines
Odd line
Even lines
Odd line
Even lines

I want to delete even rows

$ sed -i -e 'n; d' number.txt
$ cat number.txt
Odd line
Odd line
Odd line
Odd line
Odd line

I want to delete odd rows

$ sed -i -e '1d; n; d' number.txt
$ cat number.txt
Even lines
Even lines
Even lines
Even lines
Even lines

Replacement

I want to replace a specific character string with a character string containing spaces at once

All I had to do was escape the space.

#Preparation
$ pen='this is a pen.\n'
$ printf $pen"%.s" {1..10} > pen.txt
$ cat pen.txt
this is a pen.
this is a pen.
this is a pen.
this is a pen.
this is a pen.
this is a pen.
this is a pen.
this is a pen.
this is a pen.
this is a pen.

#Replacement
$ sed -i -e "s/this is a pen./This\ is\ a\ pen\./g" pen.txt
$ cat pen.txt
This is a pen.
This is a pen.
This is a pen.
This is a pen.
This is a pen.
This is a pen.
This is a pen.
This is a pen.
This is a pen.

Recommended Posts

Memorandum of sed
Memorandum of fastText (editing)
memorandum of vi command
elasticsearch_dsl Memorandum of Understanding
A memorandum of kernel compilation
A small memorandum of openpyxl
A memorandum of using eigen3
Memorandum on Memoization of recursive series
Memorandum of saving and loading model
[Django] Memorandum of environment construction procedure
[Python] A memorandum of beautiful soup4
Memorandum on Memoization of recursive functions
A memorandum of files under conf.d
Memorandum of beginners Python "isdigit" movement
A memorandum of closure survey contents
Matplotlib memorandum
jinja2 memorandum
Python memorandum
Django memorandum
Command memorandum
Python Memorandum 2
A memorandum of using Python's input function
A memorandum of speed of arbitrary degree diagonalization
plotly memorandum
sed memo
Slackbot memorandum (1)
Memorandum of python beginners About inclusion notation
multiprocessing memorandum
Memorandum MetaTrader 5
[Linux/LPIC] Memorandum
A memorandum of understanding about django's QueryDict
ShellScript memorandum
pip memorandum
sed command
Python memorandum
pydoc memorandum
python memorandum
Pandas memorandum
python memorandum
DjangoGirls memorandum
Memorandum of Understanding when migrating with GORM
Command memorandum
Python memorandum
pandas memorandum
python memorandum
Python memorandum
A memorandum of python string deletion process
A memorandum of trouble when formatting data
Memorandum (acquisition / conversion of "user-defined" time, cross tabulation)
A memorandum of calling Python from Common Lisp
A memorandum of studying and implementing deep learning
A memorandum of extraction by python bs4 request
[Linux command] A memorandum of frequently used commands
[Data science memorandum] Handling of missing values ​​[python]