1

Should I use grep to filter a real time output? I'm not sure if this is what I should use for a real time output.

Example: command -option | grep --color 'string1\|string2'

If so, how to get also the lines after string1 and string2?

DeborahAnn
  • 81
  • 1
  • 6
  • 1
    what is a real time output? – karakfa Oct 26 '18 at 20:19
  • Welcome DeborahAnn, it would be helpful if you clarify what you mean by real time output -- what exactly do you need grep to do? also, what exactly do you mean by getting the lines after? is this a single line after a match? or n lines? or every line after a `string1` or `string2` have been found? – lucascaro Oct 26 '18 at 20:24
  • Sorry, my english is bad. With "real time output" I mean a process who prints until is terminated by user (basically a loop). – DeborahAnn Oct 26 '18 at 20:24
  • Thanks @lucascaro. I have this text in output that is being generated by a process (this process is executed until the user decide to kill it) and I am using grep to find strings of this text that contains string1 or string 2. Everytime string1 or string2 is found by grep I need to also print the line right after. So if string1 is at line 20 and string2 is at line 80, I need to print line 21 and line 82. – DeborahAnn Oct 26 '18 at 20:29
  • 1
    check on `man grep` and see if your version offers the `-a` (lines **a**fter), so `cmd|grep -a2 's1\|s2'`. Good luck. – shellter Oct 26 '18 at 20:29
  • @shellter it should be upper case `A`, lowercase `a` means something else. – karakfa Oct 26 '18 at 20:41
  • Thanks to everyone! – DeborahAnn Oct 26 '18 at 20:42

1 Answers1

0

As @shellter mentioned, from man grep:

 -A num, --after-context=num
         Print num lines of trailing context after each match.  See also the -B and -C options.

so you would use command -option | grep -A 1 --color 'string1\|string2' to print matched lines and the line right after them.

There are plenty of other options in the manual for grep, and most other command-line programs, so I suggest getting used to running man cmd as a quick first check.

lucascaro
  • 10,640
  • 2
  • 31
  • 40