1

I have this file

    123 My name is Lorry I'm 29 years old 10000 45
    148 Name - Dan 20000 18
    895 My name is John Lazaro I like tennis 30000 63

And I want to print only last numbers in a line

45
18
63

Is it possible to do that?

UPDATE: Now I want to print

10000
20000
30000
3

You can try using awk to print last filed:

awk '{ print $NF }' file

and for printing field next to the last:

awk '{ print $(NF - 1) }' file
4

In your case, the last (space-separated) word happens to be a number. If that's always the case, then your problem can be reduced to printing the last word like:

awk 'NF {print $NF}'

or

sed 's/.* //'

For the one before last:

awk 'NF >= 2 {print $(NF-1)}'

If you want to print the last word iif it's a positive decimal integer number then that becomes:

awk '$NF ~ /^[0-9]+$/ {print $NF}'

(replace $NF with $(NF-1) for the one before last).

If you want to print rightmost sequence of decimal digits in every line, then:

sed -n 's/^/./;s/.*[^0-9]\([0-9]\{1,\}\).*/\1/p'

For the one before last:

sed -n 's/^/./;s/.*[^0-9]\([0-9]\{1,\}\)[^0-9].*[0-9].*/\1/p'
0
1

If someone need my updated output just type this

awk '{print $(NF-1)}' file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.