On 31/10/2012 16:40, LuKreme wrote:
> I want to grep for words in a file that contain 'a' 'b' and 'c' in any order. I also want to find words that contain two c's, even if not together (so access and chance).
> I might even want words with two c's AND a and b, again in any order.
> I feel like I am forgetting something basic.
As for most of these things, as text filter is the best solution. This:
#! /usr/bin/perl
use strict;
while (<>) {
print;
my @hits;
my @words = split /\b/, $_;
for (@words) {
push @hits, $_ if /a/i and /b/i and /c/i or /c.*c/i;
}
printf "• Found: %s\n", join ", ", @hits if @hits;
undef @hits;
}
will turn
The accountant sat in the back of the
cab wearing a black cocked hat
into
The accountant sat in the back of the
• Found: accountant, back
cab wearing a black cocked hat
• Found: cab, black, cocked
JD