Find files with a specific 2-line pattern using awk

May 7, 2014 in answer

0 votes, 0.00 avg. rating (0% score)

ANSWER:

If your version of grep supports PCRE mode (-P) you could try a multiline match that finds instances of @BOND that are followed (after only a newline) by @SUBSTRUCTURE e.g.

grep -lzP 'Q@BONDEnQ@SUBSTRUCTUREE' *

The Q and E may be unnecessary in this case but are intended to force literal matching (in case @, >, < have special meaning in the Perl regex syntax). The -l tells grep to list the matching files instead of printing the match.

You could express the second part of the match as a lookahead but I don't think it has any advantage in this case

grep -lzP 'Q@BONDEn(?=Q@SUBSTRUCTUREE)' *

Equivalent expressions in pcregrep (which is not part of the standard Ubuntu system, but obtainable from the repository) would be something like

pcregrep -lM 'Q@BONDEnQ@SUBSTRUCTUREE' *

and

pcregrep -lM 'Q@BONDEn(?=Q@SUBSTRUCTUREE)' *

You can then use the list of files as an input to the mv command e.g.

grep -lzP 'Q@BONDEnQ@SUBSTRUCTUREE' * | xargs mv -t /path/to/newdir/

steeldriver from http://askubuntu.com/questions/462229