CLI tip 9: awk paragraph mode
awk
provides a handy shortcut to process input content paragraph wise. When RS
is set to empty string, one or more consecutive empty lines is used as the input record separator. Consider the below sample file:
$ cat para.txt
hi there
how are you
2 apples
12 bananas
blue sky
yellow sun
brown earth
Here are some simple examples to filter paragraphs based on some criteria:
# paragraphs containing 'sun'
$ awk -v RS= '/sun/' para.txt
blue sky
yellow sun
brown earth
# paragraphs containing any digit character
$ awk -v RS= '/[0-9]/' para.txt
2 apples
12 bananas
# print the first paragraph
$ awk -v RS= 'NR==1' para.txt
hi there
how are you
See Paragraph mode section from my GNU awk ebook for more examples and corner cases.
Video demo:
See my CLI text processing with GNU awk ebook if you are interested in learning about the GNU awk
command in more detail.