Ubuntu – ny way to make the cut command read the last field only

command linecut-command

I have a space seperated string which is output by a command, which I like to pipe to cut, using -fd ' ' to split on spaces. I know I can use -f <n> to display field number <n>, but can I make it display the last field if I don't know the length of the string?

Or do I need to use a more flexible text editing tool like sed or awk?

Best Answer

No cut can't do that. You could use two rev commands, like

echo 'foo bar baz' | rev | cut -d' ' -f1 | rev

but it's usually easier to use awk:

echo 'foo bar baz' | awk '{print $(NF)}'
Related Question