1

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!

1 Answers1

0
$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?

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102