[英]Printing the last column of a line in a file
I have a file that is constantly being written to/updated.我有一个不断被写入/更新的文件。 I want to find the last line containing a particular word, then print the last column of that line.
我想找到包含特定单词的最后一行,然后打印该行的最后一列。
The file looks something like this.该文件看起来像这样。 More A1/B1/C1 lines will be appended to it over time.
随着时间的推移,将添加更多 A1/B1/C1 行。
A1 123 456
B1 234 567
C1 345 678
A1 098 766
B1 987 6545
C1 876 5434
I tried to use我试着用
tail -f file | grep A1 | awk '{print $NF}'
to print the value 766, but nothing is output.打印值 766,但没有输出任何内容。
Is there a way to do this?有没有办法做到这一点?
You don't see anything, because of buffering.由于缓冲,您什么也看不到。 The output is shown, when there are enough lines or end of file is reached.
当有足够的行或到达文件末尾时,将显示输出。
tail -f
means wait for more input, but there are no more lines in file
and so the pipe to grep
is never closed. tail -f
表示等待更多输入,但file
中没有更多行,因此grep
的管道永远不会关闭。
If you omit -f
from tail
the output is shown immediately:如果省略
tail
的-f
,则立即显示输出:
tail file | grep A1 | awk '{print $NF}'
@EdMorton is right of course. @EdMorton 当然是对的。Awk can search for
A1
as well, which shortens the command line toawk也可以搜索
A1
,这将命令行缩短为
tail file | awk '/A1/ {print $NF}'
or without tail, showing the last column of all lines containing A1
或不带尾,显示所有包含
A1
行的最后一列
awk '/A1/ {print $NF}' file
Thanks to @MitchellTracy's comment, tail
might miss the record containing A1
and thus you get no output at all.感谢@MitchellTracy 的评论,
tail
可能会错过包含A1
的记录,因此您根本得不到任何输出。 This may be solved by switching tail
and awk
, searching first through the file and only then show the last line:这可以通过切换
tail
和awk
来解决,首先搜索文件,然后才显示最后一行:
awk '/A1/ {print $NF}' file | tail -n1
要打印一行的最后一列,只需使用 $(NF):
awk '{print $(NF)}'
使用awk
一种方法:
tail -f file.txt | awk '/A1/ { print $NF }'
你可以在没有 awk 的情况下使用一些管道来做到这一点。
tac file | grep -m1 A1 | rev | cut -d' ' -f1 | rev
也许这有效?
grep A1 file | tail -1 | awk '{print $NF}'
您可以在awk
完成所有操作:
<file awk '$1 ~ /A1/ {m=$NF} END {print m}'
Using Perl使用 Perl
$ cat rayne.txt
A1 123 456
B1 234 567
C1 345 678
A1 098 766
B1 987 6545
C1 876 5434
$ perl -lane ' /A1/ and $x=$F[2] ; END { print "$x" } ' rayne.txt
766
$
awk -F " " '($1=="A1") {print $NF}' FILE | tail -n 1
Use awk
with field separator -F set to a space " ".使用
awk
并将字段分隔符-F设置为空格“”。
Use the pattern $1=="A1"
and action {print $NF}
, this will print the last field in every record where the first field is "A1".使用模式
$1=="A1"
和action {print $NF}
,这将打印每个记录中第一个字段是“A1”的最后一个字段。 Pipe the result into tail and use the -n 1
option to only show the last line.将结果通过管道传输到 tail 并使用
-n 1
选项仅显示最后一行。
Not the actual issue here, but might help some one: I was doing awk "{print $NF}"
, note the wrong quotes.不是这里的实际问题,但可能对某些人有所帮助:我在做
awk "{print $NF}"
,注意错误的引号。 Should be awk '{print $NF}'
, so that the shell doesn't expand $NF
.应该是
awk '{print $NF}'
,这样外壳就不会扩展$NF
。
Execute this on the file:在文件上执行此操作:
awk 'ORS=NR%3?" ":"\n"' filename
and you'll get what you're looking for.你会得到你想要的。
ls -l | awk '{print $9}' | tail -n1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.