Linux 如何使用 bash 获取每一行的最后一个单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16616975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:57:41  来源:igfitidea点击:

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

linuxbash

提问by camilo soto

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”,但问题是这些词在不同的空间

采纳答案by Fredrik Pihl

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.

回答by Hal Canary

You can do something like this in awk:

你可以在 awk 中做这样的事情:

awk '{ print $NF }'

Edit: To avoid empty line :

编辑:为避免空行:

awk 'NF{ print $NF }'

回答by abasu

there are many ways. as awksolutions 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' <file>

you can avoid sedalso and go for a whileloop.

您也可以避免sed并进行while循环。

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

它被称为参数扩展

回答by higuaro

Another way of doing this in plain bash is making use of the revcommand 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 cutusing space as the delimiter, take the first field that cutproduces and then you reverse the token again, use tr -dto delete unwanted chars and tragain 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" ","

回答by rici

You can do it easily with grep:

您可以使用 grep 轻松完成:

grep -oE '[^ ]+$' file

(-Euse extended regex; -ooutput only the matched text instead of the full line)

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

回答by rubicks

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

References:

参考: