InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Showing a Paragraph of Text After a Found Phrase in bash
Bash

Showing a Paragraph of Text After a Found Phrase in bash

InfinityCoder February 19, 2017

You are searching for a phrase in a document, and want to show the paragraph after the found phrase.

We’re assuming a simple text file, where paragraph means all the text between blank lines, so the occurrence of a blank line implies a paragraph break.

Given that, it’s a pretty short awk program:

1
2
3
4
5
6
$ cat para.awk
/keyphrase/ { flag=1 }
{ if (flag == 1) { print $0 } }
/^$/ { flag=0 }
$
$ awk -f para.awk < searchthis.txt

There are just three simple code blocks.

The first is invoked when a line of input matches the regular expression (here just the word “keyphrase”). If “keyphrase” occurs anywhere within a line of input, that is a match and this block of code will be executed.

All that happens in this block is that the flag is set.
The second code block is invoked for every line of input, since there is no regular expression preceding its open brace.

Even the input that matches “keyphrase” will also be applied to this code block (if we didn’t want that effect, we could use a continue statement in the first block).

All this second block does is print the entire input line, but only if the flag is set.
The third block has a regular expression that, if satisfied, will simply reset (turn off) the flag.

That regular expression uses two characters with special meaning—the caret (^), when used as the first character of a regular expression, matches the beginning of the line; the dollar sign ($), when used as the last character, matches the end of the line.

So the regular expression ^$ means “an empty line” because it has no characters between the beginning and end of the line.
We could have used a slightly more complicated regular expression for an empty line to let it handle any line with just whitespace rather than a completely blank line.
That would make the third line look like this:

1
/^[:blank:]*$/ { flag=0 }

Perl programmers love the sort of problem and solution discussed in this recipe, but we’ve implemented it with awk because Perl is (mostly) beyond the scope of this book.

If you know Perl, by all means use it. If not, awk might be all you need.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding Files Across Symbolic Links in bash
You issued a find command to find your .mp3 files …

Finding Files Across Symbolic Links in bash

Keeping Only a Portion of a Line of Output in bash
You want to keep only a portion of a line …

Keeping Only a Portion of a Line of Output in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap