print last field in a record, 2nd last. 
Author Message
 print last field in a record, 2nd last.

{print $NF} will print the last field
{print $(NF - 1)} will print the penultimate field
etc ...


|I was hoping this would be covered in the FAQ, I've got a unix text
|file.  Each line has a variable number of space separated entries,
|how do I print the last one, or the second last one?  I was hoping
|there would be a wildcard so I could just use the { print $1 } type
|of thing.

--

PO Box 220, Whiteknights, Reading, | Phone: +44-118-9875123 ext 7594
Berkshire, RG6 6AF, United Kingdom | Fax:   +44-118-9750203                
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WWW: http://www.*-*-*.com/ ~spr96phh/pete.html Use lynx - you know you want to!



Tue, 08 Feb 2000 03:00:00 GMT  
 print last field in a record, 2nd last.

Quote:

>Hi all,
>I was hoping this would be covered in the FAQ, I've got a unix text
>file.  Each line has a variable number of space separated entries,
>how do I print the last one, or the second last one?  I was hoping
>there would be a wildcard so I could just use the { print $1 } type
>of thing.

There's a very useful built-in variable, NF, which tells you the
number of fields in the line. This can be used to achieve what you
require:

{
print $NF   # This prints the last field
second_last=NF-1
print $second_last # And this prints the penultimate field on the line

Quote:
}

Remove NOJUNK from e-mail address before replying


Tue, 08 Feb 2000 03:00:00 GMT  
 print last field in a record, 2nd last.

Quote:

> I was hoping this would be covered in the FAQ, I've got a unix text
> file.  Each line has a variable number of space separated entries,
> how do I print the last one, or the second last one?  I was hoping
> there would be a wildcard so I could just use the { print $1 } type
> of thing.

Tried on solaris 2, standard awk. If it works there, it should work
under all awks... ;)

awk '{ print $NF }' <filename> # this should print the last item
awk '{ print $(NF-1) }' <filename> # This should print the next-to-last item

//Ingvar
--
Sysadmin, disgruntled, unpolite.
I don't speak for my employer nor do they speak for me.



Tue, 08 Feb 2000 03:00:00 GMT  
 print last field in a record, 2nd last.



Quote:
>Hi all,

>I was hoping this would be covered in the FAQ, I've got a unix text
>file.  Each line has a variable number of space separated entries,
>how do I print the last one, or the second last one?  I was hoping
>there would be a wildcard so I could just use the { print $1 } type
>of thing.

{ print $NF }
{ print $(NF-1) }

--
Bill Marcum    bmarcum at iglou dot com
"Someone forgot to put the shoggoth out.  There's ichor all over the floor."



Wed, 09 Feb 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Strange Output if Getting last Field or 2nd to the last field.

2. Print Last 10 records

3. print the last field on a line

4. Processing 2nd Last Line

5. Deleting last record of dataset ?

6. Scanning 32k Record for the last nonblank character

7. process only last record using awk

8. Last record in file

9. Parsing from the last record to the first?

10. Automatically let a list box show the last record

11. READING LAST RECORD

12. Open browse on Last Record

 

 
Powered by phpBB® Forum Software