Frederik Van Brussel Frederik Van Brussel - 6 months ago 27
Bash Question

Make oneliner of sed-command

After a lot of trying the past day, I can't make following command work on 1 line:

sed '/'"$var1"'/ {n;n;a '\'"$var2"\'' \\
}' tempproject.cfg


when i run this like above, it matches $var1 and replaces the 3rd line after it with $var2.

example of what the sed command should do:

var1=c
var2=hello
a a
b b
c c
d => sed '/'"$var1"'/ {n;n;a '\'"$var2"\'' \\ => d
e }' tempproject.cfg e
f 'hello' \
g g
h h


when i put the command on 1 line i get the following error:

sed: -e expression #1, char 0: unmatched `{'


Thanks in advance!

Answer
$var1=c
$var2=hello
$sed "/$var1/{n;n;n;s/.*/'$var2' \\\  /}" tempproject.cfg

should give you

a
b
c
d
e
'hello' \  
g
h
i

Why use three backslash?