How do I get the last word in each line with bash

LinuxBash

Linux Problem Overview


For example i have a file:

$ cat file

i am the first example.

i am the second line.

i do a question about a file.

and i need:

example, line, file

i intent with "awk" but the problem is that the words are in different space

Linux Solutions


Solution 1 - Linux

Try

$ awk 'NF>1{print $NF}' file
example.
line.
file.

To get the result in one line as in your example, try:

{
    sub(/\./, ",", $NF)
    str = str$NF
}
END { print str }

output:

$ awk -f script.awk file
example, line, file, 

Pure bash:

$ while read line; do [ -z "$line" ] && continue ;echo ${line##* }; done < file
example.
line.
file.

Solution 2 - Linux

You can do it easily with grep:

grep -oE '[^ ]+$' file

(-E use extended regex; -o output only the matched text instead of the full line)

Solution 3 - Linux

You can do something like this in awk:

awk '{ print $NF }'

Edit: To avoid empty line :

awk 'NF{ print $NF }'

Solution 4 - Linux

Another way of doing this in plain bash is making use of the rev command like this:

cat file | rev | cut -d" " -f1 | rev | tr -d "." | tr "\n" ","

Basically, you reverse the lines of the file, then split them with cut using space as the delimiter, take the first field that cut produces and then you reverse the token again, use tr -d to delete unwanted chars and tr again to replace newline chars with ,

Also, you can avoid the first cat by doing:

rev < file | cut -d" " -f1 | rev | tr -d "." | tr "\n" ","

Solution 5 - Linux

tldr;

$ awk '{print $NF}' file.txt | paste -sd, | sed 's/,/, /g'

For a file like this

$ cat file.txt
The quick brown fox
jumps over
the lazy dog.

the given command will print

fox, over, dog.

How it works:

  • awk '{print $NF}' : prints the last field of every line
  • paste -sd, : reads stdin serially (-s, one file at a time) and writes fields comma-delimited (-d,)
  • sed 's/,/, /g' : substitutes "," with ", " globally (for all instances)

References:

Solution 6 - Linux

there are many ways. as awk solutions shows, it's the clean solution

sed solution is to delete anything till the last space. So if there is no space at the end, it should work

sed 's/.* //g' <file>

you can avoid sed also and go for a while loop.

while read line
do [ -z "$line" ] && continue ;
echo $line|rev|cut -f1 -d' '|rev
done < file

it reads a line, reveres it, cuts the first (i.e. last in the original) and restores back

the same can be done in a pure bash way

while read line
do [ -z "$line" ] && continue ;
echo ${line##* }
done < file

it is called parameter expansion

Solution 7 - Linux

If you want to start a perl one-liner for last word:

perl -lane 'print "$F[-1]"'

... where -a gives autosplit into @F and $F[-1] is the last element.

To turn into list with commas:

perl -ane 'print "$F[-1]" . (eof() ? "\n":",")'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncamilo sotoView Question on Stackoverflow
Solution 1 - LinuxFredrik PihlView Answer on Stackoverflow
Solution 2 - LinuxriciView Answer on Stackoverflow
Solution 3 - LinuxHal CanaryView Answer on Stackoverflow
Solution 4 - LinuxhiguaroView Answer on Stackoverflow
Solution 5 - LinuxrubicksView Answer on Stackoverflow
Solution 6 - LinuxabasuView Answer on Stackoverflow
Solution 7 - LinuxsteveslivaView Answer on Stackoverflow