Can we print the last word of each line in linux using sed command

awksedtext processing

suppose , if there is a file consisting of following lines , if they are

12345 567 7878 66

   er3   t45t y6y46y 


 4y6 y656y y5y

   46y6 65y7 y66uyuy

 yy46y6y

The output has to look like:

66

y6y46y

y5y

y66uyuyy

y46y6y

I have tried the command sed 's/.* //g' filename and several other sed commands, but it is not working.

Can I know what is the exact sed command?

Best Answer

awk '{print $NF}'
sed 's/[[:blank:]]*$//;s/.*[[:blank:]]//'

That would still print an empty line for every blank line. To avoid it:

awk 'NF{print $NF}'
sed 's/[[:blank:]]*$//;s/.*[[:blank:]]//;/./!d'
Related Question