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

Printing the last x num of fields in var length record

Status
Not open for further replies.

Padre764

Programmer
Jul 30, 2002
10
US
Hello,

I am trying to create a nawk script to do the following: print the $1, $2, $(NF-1), $NF (basically print the first two fields and then the last two fields where the number of fields for each row varies). When I try to run this I get an error ("Trying to access field -1"). Any suggestions?

Thanks,
Padre
 
[tt]
nawk 'NF>1 {print $1,$2,$(NF-1),$NF}'
[/tt]

Jean Pierre.
 
Something like this ?
nawk '
NF>3{print $1,$2,$(NF-1),$NF;next}
{printf "NF=%d: %s\n",NF,$0}
' /path/to/inputfile
BTW be sure FS is correct for this file.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top