A memorandum for when you want to replace data in bulk using sed. Will be added at any time.
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
$ sed -i -e 'n; d' number.txt
$ cat number.txt
Odd line
Odd line
Odd line
Odd line
Odd line
$ sed -i -e '1d; n; d' number.txt
$ cat number.txt
Even lines
Even lines
Even lines
Even lines
Even lines
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