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?

  • You could try installing the gnu versions of those tools. For example, perzl.org/aix/index.php?n=Main.Sed – EightBitTony Jun 5 '17 at 11:55
  • 1
    I don't know much about aix, but if you have awk you can alternativelly do awk '/pattern/{getline;print;exit} or awk '/pattern/{p=1;next}p{print;exit} – George Vasiliou Jun 5 '17 at 12:04
  • #George Vasiliou, I understood the first usage of awk which you mentioned. in second usage here, is p normal variable or is it means something ... can you make me understand .... #thank you – Sriram P Jun 5 '17 at 13:05
  • Requests for learning materials (tutorials, how-tos etc.) are off topic. man sed on your corresponding platform, as well as [sed] [aix] questions on this site would be a good starting point. – Jeff Schaller Jun 5 '17 at 13:09
  • Jeff Schaller, Thank you for commenting, I just edited the question. – Sriram P Jun 5 '17 at 13:14

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
        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)

| improve this answer | |

I think SED is may not be the great option to search for strings in AIX/*nix systems. It is mostly used for editing file streams.

You need to use grep to search for a pattern in a file.

grep -A 1 "pattern" file.txt would give you the next line after the matching pattern.

Thanks, Arun

| improve this answer | |
  • 1
    Not by default on AIX it won't. -A is a gnu feature of grep. – EightBitTony Jun 5 '17 at 11:55
  • The print var is different in AIX instead of Linux the correct way would be to do the following. sed -n '/pattern/,$p' $RESULTSFILE – Arun Kumar Jun 5 '17 at 12:01
  • @EightBitTony sorry, I was not aware of it. – Arun Kumar Jun 5 '17 at 12:02
  • 1
    AIX's print function in sed is the same as everyone else's. p – Jeff Schaller Jun 6 '17 at 20:08

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.