This is a bit complicated for me.
My scenario in MyFile:
Search string1,
When string1 is found, grep the line containing string1 , go back over that line in upward direction and grep the first line containing string2 .
Hello MadeInGermany,
I read that question mark is used for reverse direction search but I have no more idea.
I just searched on multiple boards with "multiple string search" keyword for this but no luck.
Your directions didn't say anything about changing string1 to the string test in the output to be produced??? And, if we do the grep 's in the order you specified, the lines containing string1 should be printed before the lines containing string2. Why isn't the desired output:
testtt 11223344
His email address alfredo@alfredo.ru
testtt 55667788
His email address daniel@alfredo.ru
? Is your sample output wrong or are your directions wrong?
If you want to do it with one process instead of two (or if you don't like the separator lines that grep -B produces, you could also try ed or awk (written to match your original specification):
which, with your sample input, produces the output:
Following output from "ed" script:
testtt 11223344
His email address alfredo@alfredo.ru
testtt 55667788
His email address daniel@alfredo.ru
Following output from "awk" script:
testtt 11223344
His email address alfredo@alfredo.ru
testtt 55667788
His email address daniel@alfredo.ru
If you are using Python already why don't you do it on it?
import re
string1 = re.compile('@')
string2 = re.compile('testtt')
with open("test.file") as f:
for line in f:
if string1.search(line):
keep = line.rstrip()
continue
if string2.search(line):
print("{0}{1}".format(line, keep))