Friday, November 12, 2010

Use of tail and head


Have you ever faced a situation where you have a big text file but you are interested in a small portion of it ?
I had a situation like that a few days back . I had logs of a server of one hour duration but i was interested in only two minutes of that. While surfing I found a technique which proved very useful .

Suppose that I have logs from 09:00:00 to 10:00:00 in a file say 2010-11-12-09 but you are only interested only in logs from 09:34:54 to 09:36:54 then this is how you can do it very easily…

First grep for some text you know you will only find at  09:34:54 and get the line number for that (in my case I had timestamp i.e. 09:34:54 itself).

grep -n "0934:54" 2010-11-12-09

Say this is 100000 
Similarly find line number for 09:36:54 

grep -n "09:36:54" 2010-11-12-09

Say this is 123456
Now you can use tail and head commands to extract the data you needed.

tail -n +100000 2010-11-12-09 | head -n 23456 > filename

Here tail will give us the whole file data starting fro line number 100000 and on that data head will read first 23456 lines and '>' will send that to the filename you specify .

Now you can do whatever operation on that file as you want.  

No comments:

Post a Comment