Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a long sed command be broken over several lines?

I have sed command that is very long

sed -i 's/append ro initrd=initrd.img quiet splash nbdport=2000/append ro initrd=initrd.img quiet splash nbdport=2000 video=LVDS-1:d/g' /var/lib/tftpboot/ltsp/i386/pxelinux.cfg/default

Can it be broken over several lines to make it more clear what it does?

E.g. something like?

sed -i 's/
append ro initrd=initrd.img quiet splash nbdport=2000
/
append ro initrd=initrd.img quiet splash nbdport=2000 video=LVDS-1:d
/g'
/var/lib/tftpboot/ltsp/i386/pxelinux.cfg/default
like image 613
Sandra Schlichting Avatar asked Nov 10 '11 11:11

Sandra Schlichting


2 Answers

sed 's/[long1][long2]/[long3][long4]/' file.txt

you can use the usual backslash for spreading the expression on multiple lines. It is important though that the lines following a backslash do not feature a space at the beginning.

sed 's'/\
'[long1]'\
'[long2]'\
'/'\
'[long3]'\
'[long4]'\
'/' file.txt
like image 75
Raffael Avatar answered Nov 11 '22 15:11

Raffael


A couple of ways you can make this smaller. If you are just appending the text to the end of the line, you can use sed like this:

sed -i '/append ro initrd=initrd.img quiet splash nbdport=2000/s/$/ video=LVDS-1:d' ...

Otherwise, use shell variables to split it up a bit.

PXE_BOOT_FILE=/var/lib/tftpboot/ltsp/i386/pxelinux.cfg/default
SEARCH_PATTERN='append ro initrd=initrd.img quiet splash nbdport=2000'
REPLACE_PATTERN="$SEARCH_PATTERN video=LVDS-1:d"
sed -i "s/$SEARCH_PATTERN/$REPLACE_PATTERN/g" "$PXE_BOOT_FILE"
like image 31
camh Avatar answered Nov 11 '22 15:11

camh