Greppng

34 views
Skip to first unread message

LuKreme

unread,
Oct 31, 2012, 12:40:21 PM10/31/12
to Bbedit List
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.

Andrew Brown

unread,
Oct 31, 2012, 2:29:05 PM10/31/12
to bbe...@googlegroups.com
On 31 oct. 2012, at 17: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.

Are you quite sure you want to do that?

AB

John Delacour

unread,
Oct 31, 2012, 5:27:11 PM10/31/12
to bbe...@googlegroups.com
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







Ronald J Kimball

unread,
Nov 1, 2012, 12:47:52 PM11/1/12
to bbe...@googlegroups.com
True regular expressions are not well-suited to finding substrings in
arbitrary order. However, you could do this using look-ahead:

(?i)(?=[a-z]*a)(?=[a-z]*b)(?=[a-z]*c[a-z]*c)([a-z]+)

This regular expression works in Perl; I haven't tested it in BBEdit.

Ronald
Reply all
Reply to author
Forward
0 new messages