How to print only last column?

Try:

echo -e 'one two three\nfour five six\nseven eight nine' | awk '{print $NF}'

It's easier than you think.

$ echo one two three | awk '{print $NF}'
three

Try grep (shorter/simpler, but 3x slower than awk because of regex usage):

grep -o '\S\+$' <(echo -e '... seven eight nine')

Or ex (even more slower, but it prints the whole buffer after finish, more useful when it needs to be sorted or edited in-place):

ex -s +'%s/^.*\s//g' -c'%p|q!' <(echo -e '... seven eight nine')
ex +'%norm $Bd0' -sc'%p|q!' infile

To change in-place, replace -sc'%p|q!' with -scwq.

Or bash:

while read line; do arr=($line); echo ${arr[-1]}; done < someinput

Performance

Given the generated 1GB file via:

$ hexdump -C /dev/urandom | rev | head -c1G | pv > datafile

I've performed the parsing time stats (ran ~3x and took the lowest, tested on MBP OS X):

  • using awk:

    $ time awk '{print $NF}' datafile > /dev/null
    real    0m12.124s
    user    0m10.704s
    sys 0m0.709s
    
  • using grep:

    $ time grep -o '\S\+$' datafile > /dev/null
    real    0m36.731s
    user    0m36.244s
    sys 0m0.401s
    
    $ time grep -o '\S*$' datafile > /dev/null
    real    0m40.865s
    user    0m39.756s
    sys 0m0.415s
    
  • using perl:

    $ time perl -lane 'print $F[-1]' datafile > /dev/null
    real    0m48.292s
    user    0m47.601s
    sys 0m0.396s
    
  • using rev + cut:

    $ time (rev|cut -d' ' -f1|rev) < datafile > /dev/null
    $ time rev datafile | cut -d' ' -f1 | rev > /dev/null
    real    1m10.342s
    user    1m19.940s
    sys 0m1.263s
    
  • using ex:

    $ time ex +'%norm $Bd0_' -sc'%p|q!' datafile > /dev/null
    real    3m47.332s
    user    3m42.037s
    sys 0m2.617s
    $ time ex +'%norm $Bd0' -sc'%p|q!' datafile > /dev/null
    real    4m1.527s
    user    3m44.219s
    sys 0m6.164s
    $ time ex +'%s/^.*\s//g' -sc'%p|q!' datafile > /dev/null
    real    4m16.717s
    user    4m5.334s
    sys 0m5.076s
    
  • using bash:

    $ time while read line; do arr=($line); echo ${arr[-1]}; done < datafile > /dev/null
    real    9m42.807s
    user    8m12.553s
    sys 1m1.955s