简体   繁体   中英

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. I have a text file named log.txt and it contains 3 lines:

vm2014-09 Classic Forwarded: 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-11 Classic Forwarded: 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. I want to know how to make it return this:

11/25/2013 2:24

11/25/2013 3:54

11/25/2013 4:49

Thanks!

It's unclear from your question if your log.txt file has a fixed structure. If so I wouldn't use tail but this instead:

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. I get

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

You also have cut :

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

Fields 7 and 8 are the date and time, respectively. As @JayInNyc also suggests, this will only work on the given sample lines. Lines with different formats will give unexpected results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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