0

I am trying to use grep to search a large text file, and return the paragraph containing a few key words. I also want to return the surrounding lines in the result. So, for example I have the following words I am going to search for: blue, green, yellow. If I wanted to find the paragraph containing all 3 words, in the file called 'colors.txt', I tried the following code:

grep blue colors.txt | grep green | grep yellow

This only gave me the listings for yellow though, and not the ones that had yellow, green and blue, in the paragraph.

I then wanted to display the surrounding words, so used something like

grep -B 5 blue colors.txt | grep green colors.txt etc etc

In summary - I have a large text file, and I want to find the section containing the three colours, but display the lines surrounding it.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
sdawes
  • 101
  • You need to decide what you want. Do you want to "return the paragraph"? Or, do you want to return the five preceding lines (as per -B5)? Or, what? – John1024 Apr 30 '16 at 22:52
  • See [How to grep for text in a file and display the paragraph that has the text?](http://unix.stackexchange.com/q/82944) Most of the solutions there could be easily adapted to your needs. – don_crissti Apr 30 '16 at 23:29
  • Crossposting: http://stackoverflow.com/q/36960668/3776858 – Cyrus May 01 '16 at 06:59

1 Answers1

1
perl -00 -n -e 'print if (m/blue/i && m/green/i && m/yellow/i)' filename

This uses perl's paragraph-reading mode (-00) to print only paragraphs containing all three words (with case-insensitive matches).

a 'paragraph' is one or more lines of text, separated from other paragraphs by at least one blank line.

e.g. I saved the text of your question into a file, and ran this perl one-liner on it. Output is:

i am trying to use grep to search a large text file, and return the
paragraph containing a few key words. I also want to return the
surrounding lines in the result. So, for example I have the following
words I am going to search for: blue, green, yellow. If I wanted to find
the paragraph containing all 3 words, in the file called 'colors.txt', I
tried the following code:

grep blue colors.txt | grep green | grep yellow

This only gave me the listings for yellow though, and not the ones that
had yellow, green and blue, in the paragraph.

i.e. only 3 paragraphs of output, whereas your question had 7 paragraphs.

cas
  • 1
  • 7
  • 119
  • 185