Tuesday, January 15, 2013

How to extract a range of lines from a large file?

It is not advisable to use vi to view a large file as on most system vi will create a swap file of similar size. It is better to extract the lines of interest to another file. Say you want to extract lines 20001 to 30000 from a log file. There are many ways to accomplish that and I am going to show you 2 of them here.

sed (my preferred way)
sed -n -e '20001,30000p' log.txt > /tmp/log-small.txt
-n means don't print anything unless the p command is used. -e means execute the following expression (those inside '....'). 20001,30000p means for lines 20001 to 30000 inclusive, execute the command p, which prints them to the console.

the head & tail way
head -30000 log.txt | tail -10000 > /tmp/log-small.txt

The head command will output lines 1 to 30000 and pipe to tail which will print the last 10000 lines, i.e. lines 20001 to 30000.
As you can see, the head & tail way requires some mental arithmetics and so it is not my preferred way.

No comments: