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

前端 未结 6 1386
我在风中等你
我在风中等你 2020-12-07 22:35

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:

         


        
6条回答
  •  被撕碎了的回忆
    2020-12-07 22:51

    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.
    

提交回复
热议问题