endlesslearning
endlesslearning

Reputation: 93

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!

Upvotes: 0

Views: 454

Answers (2)

Manny D
Manny D

Reputation: 20724

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.

Upvotes: 0

NYRecursion
NYRecursion

Reputation: 2191

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

Upvotes: 1

Related Questions