cut a field, but with reverse order

Hi Everyone,

I have one a.txt:

a b 001 c
b b 002 c
c c, not 002 c

The output should be

001
002
002

If i use cut -f 3 -d' ', this does not work on the 3rd line, so i thought is any way to cut the field counting from the end? or any perl thing can do this?:confused:

Thanks

Use code tags next time please.

awk '{print $(NF-1)}' infile
001
002
002

I found the solution already, use "reverse split" :slight_smile:

If you are thinking of using perl then you donot need to use reverse split either

cat abc.txt
a b 001 c
b b 002 c
c c, not 002 c
~$ cat abc.txt | perl -e 'while(<>){ my @cols  = split; print "$cols[-2]\n";}'
001
002
002

HTH,
PL