AIX – How to Work with Multiple sed Commands

aixsed

I want to search an pattern and I need the next following line of that pattern.

In Linux I had tried as below, and it worked.

sed -n '/pattern/{N;p}' ouputfile.txt

But in AIX, this is not working and throwing error as

0602-404 Function cannot be parsed

How to achieve this in AIX?

Best Answer

You are missing a semicolon after the p before the `}'

sed -n '/pattern/{N;p;}' ouputfile.txt

You could also write it stringed as multiple -e commands:

sed -n -e '/pattern/{' -e 'N;p' -e '}' ouputfile.txt

And the safest & clearest way is to lay it out across lines as this method affords you to place in-line comments in the sed code:

sed -ne '
    # lines matching pattern
    /pattern/{
        N;      # grab the next line into the pattern space
        p;      # print the pattern space holding the current+next line
    }
' outputfile.txt

(don't forget the ; in between the N/p and # commands)

Related Question