在 bash/sed/awk 中提取文件的最后一个字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24159417/
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-09-18 10:39:10  来源:igfitidea点击:

Extract last word of a file in bash/sed/awk

bashfileawksed

提问by Wamiq

I want to extract last word of a file say a.txtin bash/sed/awk.

我想a.txt在 bash/sed/awk 中提取文件的最后一个字。

How can I do it?

我该怎么做?

回答by Jotne

To get to get the last word in the last line:

要获取最后一行中的最后一个单词:

awk 'END {print $NF}' file

回答by fedorqui 'SO stop harming'

Updated.

更新。

If you want to use awkand make sure there is a word, use this:

如果您想使用awk并确保有一个词,请使用:

tac a.txt | awk 'NF{print $NF; exit}'

tacprints the file in reverse. NFin front of the {}block makes it work whenever the line is not empty. In such case, it prints the last field (NFstands for number of fields, so $NFis the last one), and then exits.

tac反向打印文件。NF{}块前面使它在行不为空时工作。在这种情况下,它打印最后一个字段(NF代表字段数,$NF也是最后一个),然后退出。

Test

测试

$ cat a
hello my name
is blabla
and this is
my comment.
                      <--- note an empty line
$ tac a | awk 'NF{print $NF; exit}'
comment.

Or also, as suggested by Kent:

或者,正如Kent建议的

awk '{w=NF?$NF:w} END{print w}' file

w=$NF?$NF:w. This is a ternary operator: if NF>0(no empty line), set wto the last field. Otherwise, keep it the way it was. Finally, in END{}, print the last saved word.

w=$NF?$NF:w. 这是一个三元运算符:if NF>0(无空行),设置w为最后一个字段。否则,保持原样。最后,在 中END{},打印最后保存的单词。



In case you want to make it with sed, you can use this, that works in case there is no empty lines at the end:

如果您想使用sed,您可以使用它,以防万一最后没有空行:

sed -n '${s/.* //; p}' a.txt

Explanation

解释

  • $stands for last line of the file. In that case, do what is inside {}.
  • s/.* //; premove everything up to last space. Then print p.
  • $代表文件的最后一行。在这种情况下,做里面的事情{}
  • s/.* //; p删除所有内容直到最后一个空格。然后打印p

回答by Avinash Raj

Try this awk command also,

也试试这个 awk 命令,

awk -v RS="
$sed -nr '${s/.* (.*)$//pg}' File_name
" '{print $NF}' file

RS="\0"turns all the records in a file to a single single record. And then {print $NF}prints the last field of that single record.

RS="\0"将文件中的所有记录转换为单个记录。然后{print $NF}打印该单个记录的最后一个字段。

回答by Kalanidhi

You can also use sed command ,

您还可以使用 sed 命令,

tail -1 myFile.txt | grep -oE '[^ ]+$'

回答by rpax

Using tail and grep :

使用 tail 和 grep :

sed -n '/[^ ]/h; ${g;s/ *$//; s/.* //p}' a.txt

回答by anishsane

sedvariant to support empty last line (if any):

sed支持空最后一行的变体(如果有):

sed -r '/\w/{s/.*\W(\w+)\W*$//;h};$!d;x;/./!d' file

回答by potong

This might work for you (GNU sed):

这可能对你有用(GNU sed):

sed -r ':a;$!{N;ba};s/.*\W(\w+)\W*$//p;d' file

Saves the last word of the current line in the hold space then deletes the line. At the end of the file it retrieves the last word and prints it out unless there was no word in which case it deletes the empty line.

将当前行的最后一个单词保存在保留空间中,然后删除该行。在文件的末尾,它检索最后一个单词并将其打印出来,除非没有单词,在这种情况下它会删除空行。

An alternative way is to slurp the whole file into memory:

另一种方法是将整个文件放入内存中:

 sed -n '/^$/!{$ s/.* \(\w*\)$//p}'

回答by jgshawkey

<a.txt grep -o '\w\+' | tail -n1

This will omit blank lines and print the last word on the last non-blank line.

这将省略空行并在最后一个非空行上打印最后一个单词。

  • /^$/! find non-blank lines
  • $ s/ on the last line substitute
  • .* (\w*)$ capture anything between the ()
  • \1 substitute line for capture group ()
  • p print it
  • /^$/! 查找非空行
  • $ s/ 在最后一行替换
  • .* (\w*)$ 捕获 () 之间的任何内容
  • \1 行替换捕获组 ()
  • p 打印出来

回答by Thor

You can also get the last word with grepand tail:

您还可以使用grepand获得最后一个词tail

##代码##