unable to use new line in sed search pattern.

hi ,
my lilo.conf is as shown below :

prompt
    default=Primary
    read-only

image=/boot/bzImage
    label=Primary
    root=/dev/md0
    append="reboot=t md=0 ip=off panic=5 quiet console=ttyS0,115200n81"
    read-only

image=/boot/bzImage
    label=Recover
    root=/dev/sda3
    append="reboot=t ep=off panic=5 quiet console=ttyS0,115200n81"

image=/boot/bzImage
    label=SecRoot
    root=/dev/sda2
    append="reboot=t ip=off panic=5 quiet console=ttyS0,115200n81"

i am writing a tool which takes as input Primary/SecRoot/Recover, based on which i should replace the line "image=/boot/bzImage".

For example if i give SecRoot as input then

image=/boot/bzImage
label=SecRoot

should get replaced by

image=/secroot/boot/bzImage
label=SecRoot

I tried many options of sed, but could not correctly implement it. any help is greatly appreciated.

Try this:

awk -v var="SecRoot" '
NR==1{s=$0;r="image=/" tolower(var);next}
$0 ~ var{sub("image=",r,s)}
{print s; s=$0}
END{print s}' file
2 Likes

If you get a chance, could you explain that awk? I'm having a desperate time wrapping my head around how it's selecting the line before "label=SecRoot"

It is working for me franklin.
Thanks a lot, for your help , could you please explain this command.

Sure.

awk -v var="SecRoot" '
NR==1{s=$0;r="image=/" tolower(var);next}
$0 ~ var{sub("image=",r,s)}
{print s; s=$0}
END{print s}' file

Explanation:

-v var="SecRoot"

Set variable var with the pattern.

NR==1{s=$0;r="image=/" tolower(var);next}

If line number is 1, assign the current line to the variable s, assign the new string to the variable r (image=/secroot) and read the next line.

$0 ~ var{sub("image=",r,s)}

If the current line matches with the pattern, substitute the previous line.

{print s; s=$0}

Print the previous line (variable s) and assign the current line to variable s.

END{print s}

Print the last line.

# var="SecRoot" ; sed "/image=\/boot\/bzImage/{s/=\(.*\)$/=\/"$(echo $var|sed 'y/PRS/prs/')"\1/}" infile
prompt
    default=Primary
    read-only
image=/secroot/boot/bzImage
    label=Primary
    root=/dev/md0
    append="reboot=t md=0 ip=off panic=5 quiet console=ttyS0,115200n81"
    read-only
image=/secroot/boot/bzImage
    label=Recover
    root=/dev/sda3
    append="reboot=t ep=off panic=5 quiet console=ttyS0,115200n81"
image=/secroot/boot/bzImage
    label=SecRoot
    root=/dev/sda2
    append="reboot=t ip=off panic=5 quiet console=ttyS0,115200n81"
 

Thank you, Franklin52!

I think this is what you wanted to do using sed:

sed "/^image=/N;/label=$var/s#image=#&/secure#"