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!

How to compare fields in different records? 1

Status
Not open for further replies.

ericpatton

Technical User
Mar 4, 2006
6
CA
Hi, I have a text file with three columns of data: time, latitude, and longitude. I want to check that all the values in the time column increment up only (i.e., 1300, 1301, etc) and report if there are any times out of order in the file. To do this, I thought that I needed to be able to compare field 1 in record n with field 1 in record n-1, then compare field 1 in record n+1 with field 1 in record n, etc. Is there any way to do this in awk?

Thanks,

~ Eric.
 
Code:
awk '$1 <= prev; {prev=$1}' myfile
If running under windoze:
Code:
awk "$1 <= prev; {prev=$1}" myfile
 
Thanks for the fast reply!

I'm unfamiliar with this variable prev; I can't find anything on this variable in my awk text. Does it always correspond to the current field in the previous record?

Thanks again,

~ Eric.
 
I'm unfamiliar with this variable prev; I can't find anything on this variable in my awk text. Does it always correspond to the current field in the previous record?

No, it's not built in; it's a user-defined variable. When the first line of the file is read, [tt]prev[/tt] will be "". When each subsequent line is read, [tt]prev[/tt] will be equal to field 1 of the previous line because of [tt]{prev=$1}[/tt].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top