I have the text, that contains some lines. So, i need to make GREP of several lines. For example, i have repeating text and i should GREP get lines, that have this repeating key-words.

grep -o "test|test2" textfile

My text:

123|never for your|test
123421|never for your|test2
123412|never for your|test3
12341|never for your|test4
12311|never for your|test2
123312312|never for your|test
123321312|never for your|test2

I should have:

123|never for your|test
123421|never for your|test2
123312312|never for your|test
123321312|never for your|test2

It works, but it doesn't work how i want. It searchs in text, all words "test" & "test2". But i want to get textblocks, like some pattern, where only after "test" comes "test2". Have you got any ideas ?


Brief shell script using sed. Makes a list of line numbers for the second case, and compares against line numbers for the first case. Prints matching pairs. Uses the first argument as the file name. Could easily be extended to take second and third arguments as patterns to match. Could save as findnext.sh, and run:

$ sh findnext.sh testfile

Should be quick as it only involves two passes over the file, and has the advantage of being completely portable.

# Line numbers matching test1
mt2=$(sed -ne '/test1/=' < $1 | tr '\n' '/')

for l in $(sed -ne '/test/=' < $1); do
    nextline=$(expr $l + 1)
    [ "${mt2#*$nextline/}" != "$mt2" ] && sed -ne $l,${nextline}p <$1

You can try grep -E or egrep. Please try it like this

#this will show lines that have test or test2
    grep -E "test|test2" file

If you want to show line that has test and test2 like this test|test2 do this

# This will show lines that has test|test2
    grep "test\|test2" file
  • i have tried, but it doesn't work. i am sorry – Valeriu Jan 21 '17 at 22:21
  • @valeriu can you paste few lines so I can see the pattern – Talal Al-Khalifa Jan 21 '17 at 22:24
  • just a moment. i will pastle it in above text – Valeriu Jan 21 '17 at 22:32
  • yes, it works. but in my text, i have repeat test2. i have tested same commands, that you have written above. i want to have test and test2 together & get lines with this together words/lines – Valeriu Jan 21 '17 at 22:41
  • 1
    @valeriu You where doing it correct just use -E instead of the -o so you can use regular expression like test|test2 means "test or test2" with the option -o you will show only the matching wich is test or test2 – Talal Al-Khalifa Jan 21 '17 at 22:46

awk may be your tool for this:

awk '/test$/, /test2$/' < block-text-lines.txt 

the general form is:

awk '/start-pattern/, /end-pattern/{command}'

But since the command block defaults to print, just the start and end patterns do the trick.

Check out man awk or the Gnu Awk User's Guide for way more detail.

  • thanks. but it doesn't work. i have tried all this commands. i have in output repeat test2. but i want to have test2 only before test. and i have test2 before test4. look at my file above – Valeriu Jan 22 '17 at 7:18
  • Sorry, I do not understand. I run your input through that awk command, above, and get the output your questions says "I should have" in your second quote. Your output has repeated test2. Can you provide a more detailed "after" example of what you are looking to get? – Greg Tarsa Jan 23 '17 at 1:08
grep -A 1 "test$" in.txt | grep -B 1 "test2$"

In grep manual

-A NUM Print NUM lines of trailing context after matching lines.

-B NUM Print NUM lines of leading context before matching lines.

The command grep -Pzo ".*test$\n.*test2$" in.txt also works, but in the manual is "This is highly experimental and grep -P may warn of unimplemented features."

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.