1

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 ?

Valeriu
  • 57
  • 2
  • 9

4 Answers4

2

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.

#!/bin/sh 
# 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
done
Chris Rees
  • 275
  • 1
  • 7
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
Talal Al-Khalifa
  • 668
  • 5
  • 12
0

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.

Greg Tarsa
  • 201
  • 2
  • 5
  • 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 07: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 01:08
0
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."

Marco
  • 101
  • 1