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

Friday, December 30, 2022

Disable automatic backslash insertion before $ when using auto-complete in bash

When using bash that is not configured properly, on using tab to auto-complete paths, it automatically escapes dollar symbols by inserting a backslash symbol before them, causing the commands to fail if run after auto-completion. 

For example: 

ls -l $HOME_DIR/code/wo (TAB) becomes 
ls -l \$HOME_DIR/code/workspace/ 

To stop this from occurring, you need to add this command to your  ~/.bashrc

shopt -u progcomp 

After this, source the .bashrc again and every time after you login, bash will not automatically add the backslash to escape $ symbol - viola - problem solved!


Related Posts Plugin for WordPress, Blogger...