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!

req: please review one-liner 1

Status
Not open for further replies.

Kipnep70

Technical User
Nov 18, 2003
81
US
I've written a one-liner and it is producing the results I want, but I'm not confident that it is written correctly.

I'm trying to print the first field of each record and omit the last line of a file.

Code:
cat clients | awk '{a=0; if (a > $NF) print $1}'

Then I realized that $NF represents the Number of Fields in the current record. So I'm not sure how this is working correctly since the last line has more the 0 fields.

Is there a better way to write/accomplish this?

Thanks,
 
Hi

Kipnep70 said:
Then I realized that $NF represents the Number of Fields in the current record.
Wrong. [tt]NF[/tt] is the number of fields; [tt]$NF[/tt] is the value of [tt]NF[/tt]th field.

What you wrote can be written as this :
Code:
awk '0>$NF{print $1}' clients
Which means, if the last field's value is less than zero, print the first field.

Kipnep70 said:
I'm trying to print the first field of each record and omit the last line of a file.
That is a completely different story. You have to store the value until you find out that it was not the last line.
Code:
awk 'NR>1{print l}{l=$1}' clients
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

And please avoid UUOC. [tt]awk[/tt] is able to read a file, no need to spoon-feed it.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top