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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making sense of {print x}{x=$0} 1

Status
Not open for further replies.

moonring

Technical User
Feb 15, 2008
38
US
Hi,

I'm trying this awk one-liner:

Code:
Input

aaa
bbb
ccc

awk '/bbb/{print x}{x=$0}' file

Output

aaa

So this code prints always the previous line, of the line where the pattern /bbb/ is found.
I'm trying to make sense of it, but it seems difficult. It looks like when /bbb/ is found, x in x=$0 should be set to the record where /bbb/ is found, instead x=$0 is set to the previous record. Counterintuitive ?

Any idea why is this so ?
 
When /bbb/ is found, x is printed before to be set to the current $0.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top