如何在每一行中使用bash获取最后一个单词?

时间:2022-02-14 07:49:54

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

我的意图是“awk”,但问题是单词在不同的空间。

6 个解决方案

#1


56  

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:

纯粹的bash:

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

#2


52  

You can do it easily with grep:

你可以用grep很容易做到:

grep -oE '[^ ]+$' file

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

(- e使用扩展正则表达式;-o只输出匹配的文本而不是整行)

#3


19  

You can do something like this in awk:

你可以用awk做这样的事情:

awk '{ print $NF }'

Edit: To avoid empty line :

编辑:避免空行:

awk 'NF{ print $NF }'

#4


9  

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

在普通bash中实现这一点的另一种方法是使用rev命令:

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 ,

基本上,你反转文件的行,然后用cut分隔它们使用空格作为分隔符,取cut生成的第一个字段,然后再次反转令牌,使用tr -d删除不需要的字符,然后再次用tr替换换行字符,

Also, you can avoid the first cat by doing:

此外,你可以通过以下方式避免第一只猫:

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

#5


4  

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

有很多方法。正如awk解决方案所示,它是干净的解决方案

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

sed解决方案是删除任何东西直到最后一个空格。所以如果最后没有空间,它应该可以工作

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

sed的s /。* / / g ' <文件>

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

您也可以避免sed,并进行一段时间循环。

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

同样的事情也可以用纯粹的bash方法实现

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

it is called parameter expansion

它被称为参数展开

#6


3  

tldr;

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
  • awk '{print $NF}':打印每一行的最后一个字段
  • paste -sd, : reads stdin serially (-s, one file at a time) and writes fields comma-delimited (-d,)
  • 粘贴-sd,:串行读取stdin (-s,一次一个文件)并写入以逗号分隔的字段(-d,)
  • sed 's/,/, /g' : substitutes "," with ", " globally (for all instances)
  • sed 's/,/, /g':替换"," with "," global "(对于所有情况)

References:

引用:

#1


56  

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:

纯粹的bash:

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

#2


52  

You can do it easily with grep:

你可以用grep很容易做到:

grep -oE '[^ ]+$' file

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

(- e使用扩展正则表达式;-o只输出匹配的文本而不是整行)

#3


19  

You can do something like this in awk:

你可以用awk做这样的事情:

awk '{ print $NF }'

Edit: To avoid empty line :

编辑:避免空行:

awk 'NF{ print $NF }'

#4


9  

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

在普通bash中实现这一点的另一种方法是使用rev命令:

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 ,

基本上,你反转文件的行,然后用cut分隔它们使用空格作为分隔符,取cut生成的第一个字段,然后再次反转令牌,使用tr -d删除不需要的字符,然后再次用tr替换换行字符,

Also, you can avoid the first cat by doing:

此外,你可以通过以下方式避免第一只猫:

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

#5


4  

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

有很多方法。正如awk解决方案所示,它是干净的解决方案

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

sed解决方案是删除任何东西直到最后一个空格。所以如果最后没有空间,它应该可以工作

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

sed的s /。* / / g ' <文件>

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

您也可以避免sed,并进行一段时间循环。

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

同样的事情也可以用纯粹的bash方法实现

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

it is called parameter expansion

它被称为参数展开

#6


3  

tldr;

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
  • awk '{print $NF}':打印每一行的最后一个字段
  • paste -sd, : reads stdin serially (-s, one file at a time) and writes fields comma-delimited (-d,)
  • 粘贴-sd,:串行读取stdin (-s,一次一个文件)并写入以逗号分隔的字段(-d,)
  • sed 's/,/, /g' : substitutes "," with ", " globally (for all instances)
  • sed 's/,/, /g':替换"," with "," global "(对于所有情况)

References:

引用: