I'm searching for two patterns from very large files and I don't want to grep more than once. I'm currently running this command to look for the patter, then if found also take the one line before and two lines after the line with the match, then remove the line separators with sed, and finally zip the output:

zgrep -B 1 -A 2 'Pattern_1' In.gz | sed '/^--$/d' | gzip >out.gz

How could I look for both pattern 1 and 2 simultaneously and print my lines to out1.gz or out2.gz, respectively. I read this thread but I'm not familiar with awk so I'm not sure if it applies to my situation.


  • I think you could achieve this kind of thing using awk maybe....
    – StefanR
    Dec 11, 2015 at 18:19

1 Answer 1


You can do:

zgrep -B1 -A2 'pat_1\|pat_2' In.gz | tee >(grep -B1 -A2 'pat_1' | \
  grep -v '^--$' | gzip >out1.gz) | grep -B1 -A2 'pat_2' | \
    grep -v '^--$' | gzip >pat_2.gz

Here tee will send the initial output of zgrep -B1 -A2 'pat_1\|pat_2' In.gz two-ways:

  • The commands within process substitution >() will work on one pattern (pat_1)

  • The output sent to STDOUT will be catched by next piped grep and will be worked on to get the output for second pattern (pat_2).

  • Thanks! This worked for me. But then it raised a question that I'm sure there's an answer to which would involve four greps. I have 2 large files that contain sequences of DNA. Line 1 in file 1 goes with line 1 in file 2, then the line 2's go together, etc. Dec 11, 2015 at 18:58
  • @DavidWells ummm..don't think i get it....could you please ask it as a new question with enough description and example ?
    – heemayl
    Dec 11, 2015 at 19:03
  • Yes i'll ask another question. Dec 11, 2015 at 19:06

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.