0

I'm chaining together multiple commands on the command line. Is there any tool that will allow me to capture output starting with a specific pattern? For example, if I have the following output produced:

line 1
line 2 
line 3
line 4
my_pattern
line 5
line 6
line 7

Is there some command that will allow me to capture the output starting with my_pattern? This result in the lines

my_pattern
line 5
line 6
line 7

being captured. The my_pattern line is not mandatory to be included.

Geo
  • 93,257
  • 117
  • 344
  • 520
  • Well this can help: [using sed/awk to select lines between two patterns occurring which is occurring multiple times](http://stackoverflow.com/q/17988756/1983854) – fedorqui May 06 '14 at 11:15
  • 1
    @devnull I did. And you, did you even check the linked question? No need to be rude. – fedorqui May 06 '14 at 11:22
  • @fedorqui I'm pretty sure that there would be much better questions that _might help_ in comparison to the linked question. And asking if you _read the question_ isn't _rude_, or is it? – devnull May 06 '14 at 11:25
  • @devnull this is one good example I found. Of course there might be better ones, but that one solves this specific problem. It is not rude the sentence itself, but it is what lies beneath. – fedorqui May 06 '14 at 11:28
  • @fedorqui Looks like you have a habit of reading way too much. Good luck. – devnull May 06 '14 at 11:30

2 Answers2

3

Sure, use sed like this:

sed -n '/my_pattern/,$p' file
my_pattern
line 5
line 6
line 7

That says, don't print anything (-n) till you find my_pattern, then print from there to the end of the file. Or in a pipeline:

.... | sed -n '/my_pattern/,$p'
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

You can use awk like this for this output redirection:

tail -f /logs/apache_error.log | awk '!found && /my_pattern/{found=1} found'

This awk will not produce any output until my_pattern is found in output and once my_pattern appears then all the output text from tail -f will be written to stdout (can be redirected to a file as well).

Testing:

printf "1\n2\n3\n4\nmy_pattern\n5\n6\n7\n" | awk '!found && /my_pattern/ {found=1} found'
my_pattern
5
6
7
anubhava
  • 761,203
  • 64
  • 569
  • 643