Saturday, August 3, 2013

Extract range of lines from text file in unix

It is possible to use command line sed to easily extract a range of lines from a text file. The command to be used is:
sed -n 16224,16482p filename > newfile


Where 16224,16482 are the start line number and end line number, inclusive. This is 1-indexed. 
-nsuppresses echoing the input as output, which you clearly don't want; 
the numbers indicate the range of lines to make the following command operate on; 
the command p prints out the relevant lines.

References:
http://stackoverflow.com/questions/83329/how-can-i-extract-a-range-of-lines-from-a-text-file-on-unix

No comments:

Post a Comment