Awk Sed Grep – Find Text String in File and Output Following Text

awkgreppipesedstdout

I am finding a lot of posts on this forum that have to do with finding various values in a text file and outputting text surrounding it. However, I don't seem to find any "stream oriented".

I want to find a particular string in a file, and output only the text that follows it until the end of the file is reached. In other words, I want something that acts like a filter that ignores the text in a file until a specific string value is reached, and then from that point on outputs text to stdout until the end of the file. I want to use stdout so I can pipe output to a file if I so chose.

Is there a Linux text utility that will help me do this and if so, how? Or if I need to write a bash shell script to accomplish that, what are the general steps and command line utilities I would use to do this?

For example, given a sample file below:

one
two
three
four
five

Suppose I wanted to output all the text after the "three" so the result would be:

four
five

NOTE: I did find this seemingly related post but as you can see it's a bit of a mess:

how to find a text and copy the text after?

Best Answer

Use awk:

awk 's;/^three$/{s=1}' file

or

awk 's;$0=="three"{s=1}' file
  • s; will print the line if variable s is true, which is the case first time after the pattern/word has been found ...
  • /^three$/{s=1} will set variable s to true (1) if pattern/word is found.
Related Question