Search text from a file and print text and one previous line too

Hi,

Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11

Thanks for your help.

One way:

awk '/pattern/{print s "\n" $0;exit}{s=$0}' file

Regards

I have no idea what HP-UX 11.11 is.. but normal *nix shell, you can do:

grep -A2 "sometext" somefile.ext

It would grab that text, and the two lines afterwards.. You can also use -B for before.

Or:

grep -B1 pattern file

or:

sed -n -e '/pattern/{x;p;x;p}' -e h file

Please "cfajohnson" What should I change in the following command to find text and its previous line.

I got command (text=start; awk '/'"$text"'/{n=2}n-->0' filename) for finding text and next line.

Thanks for your help.

Hi,

Command awk '/pattern/{print s "\n" $0;exit}{s=$0}' file only returns the first occurance of the pattern, it is not showing the next occurance.

Please help me, thanks

Remove the exit command:

awk '/pattern/{print s "\n" $0}{s=$0}' file

Regards