Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search for pattern and print previous lines?

Status
Not open for further replies.

ZorbasE

Technical User
Nov 2, 2006
2
GR
Hello, im new to the forum and to AWK...

is it pls possible to search for a pattern and print a previous line? I tried to do that by decrementing NR but
it didnt work of course. To be more specific, say that i
find a pattern on line X and if that happens i need to
print or store a field of line X-3 etc.

thank you in advance

Yannis
 
Hi

You can not step back. Must store the data temporary, until you reach the condition line :
Code:
awk '/[green][i]condition[/i][/green]/{print p[(NR+1)%3]}{p[(NR+1)%3]=$0}' /input/file

Feherke.
 
Off topic I know, but if you have GNU grep you can use grep -Bn pattern inputfile | grep -v pattern where n is the number of lines of context you want.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top