From: Orson on
I need to see lines in file containing 2 strings: each line must have
string "ABCDE" and "12345" anywhere in the line, but both must be
there. It just happens that "ABCDE" comes first and "12345" ccomes
later, but I do not need to enforce it.

I have test file:

$ cat samplefile
this is a line that does not match
12345 this is a line that does not match
ABCDE this is a line that should not match
this line ABCDE should not match
this line you 12345 should not see
ABCDE12345 you should SEE this line
this line you ABCDE should SEE 12345

I tried this:

grep -P "(ABCDE)+.(12345)+" samplefile

but I get no output. I thought this means all lines with "ABCDE" 1 or
more times followed by anything followed by "12345" one or more times.
No? Is there better way to doing this?

From: Orson on
Oh, I see problem! I need to do this:

grep -P "(ABCDE)+(.)*(12345)+" samplefile

But is there a better way to do logical AND. Basically, this is
searching for "ABCDE" AND "12345" both present in each line.


From: Dave B on
Orson wrote:

> Oh, I see problem! I need to do this:
>
> grep -P "(ABCDE)+(.)*(12345)+" samplefile
>
> But is there a better way to do logical AND. Basically, this is
> searching for "ABCDE" AND "12345" both present in each line.

A clear way is to use awk:

awk '/ABCDE/ && /12345/' samplefile

this will print all the lines where both ABCDE and 12345 appear, even if
12345 comes before ABCDE.

If you want the two patterns to appear in that order, then you have to do

grep 'ABCDE.*12345' samplefile

Is this what you need?

--
echo 0|sed 's909=oO#3u)o19;s0#0ooo)].O0;s()(0bu}=(;s#}#.1m"?0^2{#;
s)")9v2@3%"9$);so%op]t(p$e#!o;sz(z^+.z;su+ur!z"au;sxzxd?_{h)cx;:b;
s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
tb'|awk '{while((i+=2)<=length($1)-18)a=a substr($1,i,1);print a}'
From: Stephane CHAZELAS on
2008-06-20, 07:10(-07), Orson:
> Oh, I see problem! I need to do this:
>
> grep -P "(ABCDE)+(.)*(12345)+" samplefile
>
> But is there a better way to do logical AND. Basically, this is
> searching for "ABCDE" AND "12345" both present in each line.


awk '/ABCDE/ && /12345/'

sed '/ABCDE/!d;/12345/!d'

grep ABCDE | grep 12345

perl -ne 'print if /12345/ && /ABCDE/'

grep -e 'ABCDE.*12345' -e '12345.*ABCDE'
(works in that case but wouldn't if you replace 12345 with DEFGH
for instance as it would fail to match ABCDEFGH).

grep -P '(?=.*ABCDE)12345'
(but note that -P is not standard and is not widely supported)

--
St�phane
From: Orson on
> If you want the two patterns to appear in that order, then you have to do
>
> grep 'ABCDE.*12345' samplefile
>
> Is this what you need?
>
Thank you everyone for reply. This is what I wanted yes. More simple
than my '(ABCDE)+(.)*(12345)+' and works with grep. We want to use
grep because support is more familiar with grep, but perl and awk are
difficult for them.

I am curious, what is difference between 'ABCDE.*12345' and '(ABCDE)+
(.)*(12345)+'

I think it is only that ()+ is allowing multiple times and without
parameters it is allowing exactly one time? Then I don't need
parentehsis for my samplefile.