Howto extract the text between two key words in Bash

Are you having a text file with a repeating pattern of key words and want to extract the content which is written between these keywords. Your file could look like the follows

this is not interesting
hello
this is the text I Want to extract
world
also this line should be skipped
hello
I want also this text
And this one
world
ignore this text

and so on. As you can imagine Bash offers all the tools you need to do the trick. There are plenty of ways to achieve the goal of extracting the text between the keywords from your file. My favorite bash command to do so is the following one.

[sourcecode language=”bash”]
awk ‘/^hello$/,/^world$/ { print }’ input.txt > output.txt
[/sourcecode]

In case input.txt contains the example line above output.txt will only contain the lines between the key words. The keyword embracing the interesting text will remain in in the output file. You easily can remove them e.g. by using the find and replace feature of vim or any other text editor.

Life can be that easy!

Leave a Reply

Your email address will not be published. Required fields are marked *