Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I have a file that I need to extract multiple lines out of. When I find a certain keyword in the file, I need that line and the next 2 lines (three lines in total). This happens several times a file. I found this script on the internet:
sed -n '
/keyword/ {
N
/\n.*/ p
}' input > outputWhich works, but only gives me the line with the keyword and the next line. How do I get the third line?
Thanks!

I couldn't decide if I should put '2N' or if it was just another 'N', and then if the Ns could go together or the second N needed to be on a new line or if it even mattered, and I got pulled away before I could try anything. Thank you very much nails!

The grep -A option is relatively new. It's not available on the older *nix versions. Yes, it is possible to download the new version for your unix flavor at gnu.org.

yes. its GNU option. i see OP has AIX (presumably). here's another one with awk
awk 'c&&c--;/pattern/{c=2}' file. No Aix machine to test, but should work. If not do it the old fashion wayawk '/pattern/{getline;print;getline;print}' file

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |