简体   繁体   English

如何对多行文本文件重复使用tail命令?

[英]How do I use tail command repeatedly for a text file with multiple lines?

I've been reading a lot about the tail command recently and I can't say I found the solution to my problem. 我最近阅读了很多有关tail命令的信息,我不能说我找到了解决问题的方法。 I have a text file named log.txt and it contains 3 lines: 我有一个名为log.txt的文本文件,它包含3行:

vm2014-09 Classic Forwarded: Mla-site 12828034:3021:1298320 0000001110000000000 11/25/2013 2:24 vm2014-09 Classic转发:Mla-site 12828034:3021:1298320 0000001110000000000 11/25/2013 2:24

vm2014-10 Application Forwarded: Spc-site 238567034:3021:1298320 0000001110000000000 11/25/2013 3:54 vm2014-10转发的应用程序:SPC站点238567034:3021:1298320 0000001110000000000 2013/11/25 3:54

vm2014-11 Classic Forwarded: Mla-site 12828034:3021:1298320 0000001110000000000 11/25/2013 4:49 vm2014-11经典转发:Mla-site 12828034:3021:1298320 0000001110000000000 11/25/2013 4:49

I want to get the last part of the lines Now I have this code 我想获得这些行的最后一部分现在我有了这段代码

tail -c 17 log.txt

and it obviously returns 11/25/2013 4:49 which is the last line of the file. 并且显然返回了11/25/2013 4:49 ,这是文件的最后一行。 I want to know how to make it return this: 我想知道如何使其返回:

11/25/2013 2:24 11/25/2013 2:24

11/25/2013 3:54 11/25/2013 3:54

11/25/2013 4:49 2013/11/25 4:49

Thanks! 谢谢!

It's unclear from your question if your log.txt file has a fixed structure. 从您的问题尚不清楚,您的log.txt文件是否具有固定的结构。 If so I wouldn't use tail but this instead: 如果是这样,我不会使用tail而是这样:

cat log.txt | sed '/^\s*$/d' | awk '{print $(NF-1)" "$NF}'

The sed removes the blank lines, the awk returns the last two space-delimited fields. sed删除空白行, awk返回最后两个以空格分隔的字段。 I get 我懂了

11/25/2013 2:24
11/25/2013 3:54
11/25/2013 4:49

You also have cut : 您还cut

tail log.txt | cut -d ' ' -f 7,8

Fields 7 and 8 are the date and time, respectively. 字段7和8分别是日期和时间。 As @JayInNyc also suggests, this will only work on the given sample lines. 正如@JayInNyc所建议的那样,这仅适用于给定的示例行。 Lines with different formats will give unexpected results. 不同格式的行会产生意外的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM