7

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?

10

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.
9

With GNU sed:

sed 0,/three/d

Note that Linux is a kernel, it doesn't have utilities. GNU sed is the sed implementation commonly found on GNU/Linux systems like Debian GNU/Linux. GNU sed predates Linux and has been compiled to run on most Unix-like systems including most of those running Linux as their kernel. It is itself a Free, Libre and OpenSource re-implementation (with a few extensions including that 0 address) of the UNIX sed utility from the late 70s.

1

With POSIX sed you can do as:

sed -ne '/three/!d;:a;n;p;ba' inp.file

Using Perl:

perl -lne 'print if 1 <(/three/...eof)' inp.file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.