User Tools

Site Tools


sed_notes

This is an old revision of the document!


replace text between two lines with a file

Replace lines between \$start and \$end in main.txt with contents of part.txt

sed -e "$start e cat part.txt" -e "$start,$end d" main.txt

Sample command:

sed -e "1 e cat part.txt" -e "1,2 d" main.txt
$ cat main.txt
a b c
d e f
g h i
j k l

$ cat part.txt
x y z
p q r
s t u

$ start=1; end=2; sed -e "$start e cat part.txt" -e "$start,$end d" main.txt
x y z
p q r
s t u
g h i
j k l

$ start=2; end=4; sed -e "$start e cat part.txt" -e "$start,$end d" main.txt
a b c
x y z
p q r
s t u

$ start=2; end=3; sed -e "$start e cat part.txt" -e "$start,$end d" main.txt
a b c
x y z
p q r
s t u
j k l

$ start=2; end=2; sed -e "$start e cat part.txt" -e "$start,$end d" main.txt
a b c
x y z
p q r
s t u
g h i
j k l

backup with timestamp

sed "-i_asof_`date +%Y%m%d_%M%S`" -e 's/foo/bar/g' main.txt

will backup to something like main.txt_asof_20210128_2231

replace double quote with single quote

sed "s/\"/'/g"

Ref:- https://stackoverflow.com/questions/16154007/replace-all-double-quotes-with-single-quotes

Sample run:

% sed "s/\"/'/g"
a
a
"
'
'
'
sed_notes.1611864802.txt.gz · Last modified: 2021/01/28 20:13 by raju