Saturday, December 31, 2022

Extract a range of lines from a file on Linux

Say you have a huge file, but want to extract only a specific range of lines from that file into another one. There are many ways to do this, but a very simple way would be to use sed as follows:

sed -n '$start_line,$end_linep;$end_lineq' input_file.txt > output_file.txt

This will print all lines from the start_line to end_line and stop processing once it reaches end_line.
Note that the p in the above command makes sed print the line and the q makes sed end once the specific line is processed. Since we have the print command inside the quotes, we suppress automatic printing of pattern space by specifying the -n switch. For example, to extract lines 1500 - 1700, we can use this command:

sed -n '1500,1700p;1700q' input_file.txt > output_file.txt

Another simple method is to use a combination of head and tail as follows:

head -n +$end_line input_file.txt | tail -n +$start_line > output_file.txt

Here the + before the line numbers tells the head to output up to that line number and for tail to output starting at that line number. So our command above uses head to get all lines till the last required line and then uses tail to get all lines including and following the required first line so we do not need to do any calculations in our head. For example, to extract lines 1500 - 1700, we can use this command:

head -n +1700 input_file.txt | tail -n +1500 > output_file.txt

No comments:


Related Posts Plugin for WordPress, Blogger...