I would like to know the code for grep
to check for two matches one after the other.
For example I have the following text from one of the search files:
@<TRIPOS>MOLECULE ← pattern
1532 ← ID
17 17
SMALL
NO_CHARGES
I need to find the exact match of the ID which is always located one line below pattern and then retrieve the file name from which ID was located.
I used the following command:
grep -Pzo '@<TRIPOS>MOLECULE'(?:.*\n)*?\K1532' filename
but I got files containing 1532 as well as for 153284. I need code for exact ID match.
string₁\n string₂\n
appears in your data. Callingstring₁
a “pattern” andstring₂
an “ID” just confuses matters. And you say, “I need to … retrieve the file name from which ID was located.” but then you show a command that seems to be trying to outputstring₂
. Please state what outcome you want: true or false for a given filename? the matching filename from a list?string₂
? … (Cont’d) – Scott - Слава Україні Jul 05 '17 at 19:02string₂
, that’s a trivial complication. Since you already know whatstring₂
is, you can simplify this to(command-that-returns-true-or-false) && echo "string₂"
. – Scott - Слава Україні Jul 05 '17 at 19:03