sed on mac: 'extra characters after p command'

11,626

Solution 1

Add an extra semi-colon after the last p

 sed -n '/\[test\]/{n;p;n;p;}' my-file

(not related to Mac version, also fails on MSYS)

Solution 2

Additionally, if you happen to get this error when using the -i option without an extension:

> sed -i 's/foo/bar/g' file
sed: 1: "file": extra characters at the end of p command

In this case the solution is to explicitly set the extension to an empty string:

> sed -i '' 's/foo/bar/g' file

Solution 3

OSX (BSD) sed needs each command be terminated by ; or on a separate line.

So this should work:

sed -n '/\[test\]/{n;p;n;p;}' my-file

Or even this one will work:

sed -n '/\[test\]/{
n
p
n
p
}' my-file
Share:
11,626
julius
Author by

julius

Updated on June 13, 2022

Comments

  • julius
    julius almost 2 years

    On Linux I use this to get the next two lines after the [test] line:

    sed -n '/\[test\]/{n;p;n;p}' my-file
    

    On Mac I get:

    sed: 1: "/\\[test\\]/{n;p;n;p}": extra characters at the end of p command.
    

    Is there an expression that works on both platforms or on Mac a completely different command must be used?