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₂\nappears 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