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
How do I get the last word in each line with bash
提问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 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' <file>
you can avoid sed
also and go for a while
loop.
您也可以避免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 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" ","
回答by rici
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
只输出匹配的文本而不是整行)
回答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 linepaste -sd,
: readsstdin
serially (-s
, one file at a time) and writes fields comma-delimited (-d,
)sed 's/,/, /g'
:s
ubstitutes","
with", "
g
lobally (for all instances)
awk '{print $NF}'
: 打印每一行的最后一个字段paste -sd,
:stdin
串行读取(-s
一次一个文件)并写入以逗号分隔的字段(-d,
)sed 's/,/, /g'
:s
ubstitutes","
与", "
g
lobally(所有实例)
References:
参考: