我如何获得的最后一个字的使用bash每行 [英] How do I get the last word in each line with bash
本文介绍了我如何获得的最后一个字的使用bash每行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如我有一个文件:
$ cat file
i am the first example.
i am the second line.
i do a question about a file.
和我需要:
example, line, file
我的意图有过awk,但问题是这样的词语在不同的空间
i intent with "awk" but the problem is that the words are in different space
推荐答案
尝试
$ 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 }
输出:
$ awk -f script.awk file
example, line, file,
纯bash的:
$ while read line; do [ -z "$line" ] && continue ;echo ${line##* }; done < file
example.
line.
file.
这篇关于我如何获得的最后一个字的使用bash每行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文