0

I am having the following piece of text.

TRACE(FOO, CAT,
    INFO, "Hello World")

If I use grep -r "FOO" file.txt, grep only prints the first line. I need the whole line to be matched. I only have FOO as the search key. I want to do this with grep.

I tried grep --after-context=1, but in cases where my test is TRACE(FOO, CAT, INFO, "HELLO WORLD" (i.e no line break after the CAT) it is unnecessarily showing the next line too.

Can you please help me with doing it.

Chid

CHID
  • 1,625
  • 4
  • 24
  • 43

1 Answers1

2
sed -n -e '/FOO.*)/p' -e '/FOO[^)]*$/{p;n;p}' x.txt

if sed is also acceptable. and -n quiet the output, p print the line, n goes to the next line.

combine the 2 lines,

 sed -n -e '/FOO.*)/p' -e '/FOO[^)]*$/{N;s/\n//p;n;p}' x.txt
Dyno Fu
  • 8,753
  • 4
  • 39
  • 64