Skip to main content
added 362 characters in body
Source Link
nisetama
  • 1.2k
  • 14
  • 7

If patterns.txt contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if($0!~i)next}1' patterns.txt -

To search for fixed stringsOr this matches substrings instead of regular expressions, use this variant:

awk 'NR==FNR{a[$0];next}{for(i in a)if(!index($0,i))next}1' patterns.txt -

To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1], or with ARGIND==1 in gawk.

These functions print the lines of STDIN which contain each string specified as an argument as a substring. ga stands for grep all and gai ignores case.

ga(){ awk 'FILENAME==ARGV[1]{a[$0];next}{for(i in a)if(!index($0,i))next}1' <(printf %s\\n "$@") -; }
gai(){ awk 'FILENAME==ARGV[1]{a[tolower($0)];next}{for(i in a)if(!index(tolower($0),i))next}1' <(printf %s\\n "$@") -; }

If patterns.txt contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if($0!~i)next}1' patterns.txt -

To search for fixed strings instead of regular expressions, use this variant:

awk 'NR==FNR{a[$0];next}{for(i in a)if(!index($0,i))next}1' patterns.txt -

To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.

If patterns contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if($0!~i)next}1' patterns -

Or this matches substrings instead of regular expressions:

awk 'NR==FNR{a[$0];next}{for(i in a)if(!index($0,i))next}1' patterns -

To print all instead of no lines of the input in the case that patterns is empty, replace NR==FNR with FILENAME==ARGV[1], or with ARGIND==1 in gawk.

These functions print the lines of STDIN which contain each string specified as an argument as a substring. ga stands for grep all and gai ignores case.

ga(){ awk 'FILENAME==ARGV[1]{a[$0];next}{for(i in a)if(!index($0,i))next}1' <(printf %s\\n "$@") -; }
gai(){ awk 'FILENAME==ARGV[1]{a[tolower($0)];next}{for(i in a)if(!index(tolower($0),i))next}1' <(printf %s\\n "$@") -; }
Bounty Awarded with 50 reputation awarded by don_crissti
Adapted my answer edit to re-insert information "lost" accidently by my previous edit. Thanks for the hint, @don_cristi
Source Link
Alex Stragies
  • 6.2k
  • 2
  • 35
  • 58

If patterns.txt contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if($0!~i)next}1' patterns.txt -

To search for fixed strings instead of regular expressions, replace match with index.use this variant:

awk 'NR==FNR{a[$0];next}{for(i in a)if(!index($0,i))next}1' patterns.txt -

To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.

If patterns.txt contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if($0!~i)next}1' patterns.txt -

To search for fixed strings instead of regular expressions, replace match with index.

To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.

If patterns.txt contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if($0!~i)next}1' patterns.txt -

To search for fixed strings instead of regular expressions, use this variant:

awk 'NR==FNR{a[$0];next}{for(i in a)if(!index($0,i))next}1' patterns.txt -

To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.

Incorporated good optimization hint from @StéphaneChazelas
Source Link
Alex Stragies
  • 6.2k
  • 2
  • 35
  • 58

If patterns.txt contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if(!match($0,i)!~i)next}1' patterns.txt -

To search for fixed strings instead of regular expressions, replace match with index.

To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.

If patterns.txt contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if(!match($0,i))next}1' patterns.txt -

To search for fixed strings instead of regular expressions, replace match with index.

To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.

If patterns.txt contains one pattern per line, you can do something like this:

awk 'NR==FNR{a[$0];next}{for(i in a)if($0!~i)next}1' patterns.txt -

To search for fixed strings instead of regular expressions, replace match with index.

To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.

added 58 characters in body
Source Link
nisetama
  • 1.2k
  • 14
  • 7
Loading
Source Link
nisetama
  • 1.2k
  • 14
  • 7
Loading