14

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
Sandra Schlichting
  • 25,050
  • 33
  • 110
  • 162

6 Answers6

8
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
Raffael
  • 19,547
  • 15
  • 82
  • 160
5

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"
camh
  • 40,988
  • 13
  • 62
  • 70
  • What is s/$/ means after the needle? Can you explain a bit more? – Micromega Nov 10 '11 at 12:12
  • @Jitamaro: `s/$/xxx/` is a way to append `xxx` to the end of the line. In a regex `$` matches the end of the line. You can also do `s/^/xxx/` to prepend `xxx` to the start of the line. – camh Nov 10 '11 at 12:14
  • 1
    @hitamaro: substitute. It will only apply to lines that match the previous regex. You can also use line numbers and ranges too. Read up on sed addresses (section "Addresses") in the sed man page for more details. – camh Nov 10 '11 at 12:26
2

Yes it can, just quote it as usual:

sed 's/foo/bar/g
     s/baz/quux/g'
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Well, not really. In fact, you have to use the semi column `;` to separate commands if they are on the same line. If commands are on different lines, then no need for the `;` (but it won't hurt if it's there). – Didier Trosset Nov 10 '11 at 11:51
  • The problem is how to break the regex part. In the example is the search and replace strings very long, so it is those that I would like to break. – Sandra Schlichting Nov 10 '11 at 11:52
  • this is a cut of 2 action and request is about to cut a single s/// action. – NeronLeVelu Jul 29 '14 at 12:50
2

This might work for you:

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

or

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

N.B. the & in the Right Hand Side of the substitution represents all of the matching regex on the Left Hand Side

potong
  • 55,640
  • 6
  • 51
  • 83
1

Regex isn't meant to use such long expression, why not shortcut the needle like this:

sed -i 's/nbdport=2000/nbdport=2000 video=LVDS-1:d/g' /var/lib/tftpboot/ltsp/i386/pxelinux.cfg/default
Micromega
  • 12,486
  • 7
  • 35
  • 72
0

Sandra, you can alternatively put that large sed command in a file, say tftp.sed and invoke sed like sed -i -f tftp.sed /var/lib/tftpboot/ltsp/i386/pxelinux.cfg/default where tftp.sed file looks like:

# my very long line:
s/append ro initrd=initrd.img quiet splash nbdport=2000/append ro initrd=initrd.img quiet splash nbdport=2000 video=LVDS-1:d/g
# note: every sed command in this file must be in a separate line

As you can see above, you can have multiple sed commands inside the sed source file, just be sure they are each in a separate line.

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
  • 1
    even if it doesn not replay to the request (cutting long sed 'single' action), this is a possible alternative if the request is due to a too long line to write on a command line, not if it is in a script. – NeronLeVelu Jul 29 '14 at 12:54