What is the best way to combine these two SED commands together? The first command searches for CSP and then moves down one line. The second command puts a semicolon in column 3.

sed -ne '/CSP/{n;p;}' test1>test2
sed 's/./;/3' test2>final

The command is needed to act upon one line of data and cannot be conducted separately.

  • I just need to know in general how to add two commands together. – 985ranch Aug 23 '16 at 17:32
  • 7
    Doesn't the first one print the line following one with CSP? I think you've already combined two commands, the n and p. So would simply sed -ne '/CSP/{n;s/./;/3;p;}' work? – ilkkachu Aug 23 '16 at 17:34
  • 1
    ilkkachu is right. At a minimum you should avoid the temp file by using pipes: sed -ne '...' test1 | sed '...' > final – glenn jackman Aug 23 '16 at 17:39
  • @ilkkachu make it an answer and get reputation for it! – Nils Aug 24 '16 at 15:11
  • I'll yield under the social pressure. – ilkkachu Aug 25 '16 at 12:56

In general, sed commands can be put together separated either with newlines or with semicolons. A bit like in sh or python, actually. (Most sed commands, that is, some require the newline.)

So, to put n, p and s together, you can just use semicolons, like you already did with {n;p} in the first command. The full command in this case would be:

sed -ne '/CSP/{n;s/./;/3;p;}' test1 > final

Or you could write it with newlines instead:

sed -ne '/CSP/{n
}' test1 > final

But that's a bit annoying to do on the command line for a simple one-liner (and apparently even harder in csh), but might be more useful in case you save a sed script to a file.

| improve this answer | |
  • Good job ilkkachu! – 985ranch Aug 25 '16 at 15:21
  • GNU sed accepts ; as a command separator. BSD sed (as found on OS X, for example) requires newlines. – Kusalananda Aug 25 '16 at 15:34
  • 2
    @Kusalananda, ; can be used to separate most commands in all sed implementations, but cannot be used after certain commands like :, b, }, w or # in some implementations. For instance b a;b is meant to branch to the a;b label in POSIX seds (that will change in the next POSIX spec version though), and in # foo; bar, that ; is part of the comment in all implementations, even GNU sed (same for w file;foo.txt). sed -ne '/CSP/{n;s/./;/3;p;}' should work in all sed implementations even if IIRC POSIX currently doesn't allow it (a bug in the spec) – Stéphane Chazelas Aug 25 '16 at 15:47
  • I did test that example with the sed on OS X, actually, since I expected there was some trickery with the semicolon. – ilkkachu Aug 25 '16 at 15:59

I also found that removing some of the parameters helped when I had to use larger files and leave more of the file contents intact. I am answering this in case anyone looks this up in the future.

sed '/CSP /{n;s/./;/3;}' test1>test2
| improve this answer | |
  • Without the -n, that would print the CSP line though. – Stéphane Chazelas Aug 25 '16 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.