grep lines containing a word only twice.

Hi,

I have a query on using grep options. I tried with several options but unable to do it.

lanite:52> cat note
123 456 ab 123
ab cv 234 4566 67
ab gh tij ab 12 34 ab
ab cv dfgv ab cv ab kjhk ab ghj
sdf
dfg ab jljklj ab

Now, I need to use grep to find the line which contains the word "ab" exactly twice. Please help me.:confused:

$ awk '{for (i=1;i<=NF;i++) if ($i=="ab") A++ } {if (A=="2") print $0} A=0' urfile
dfg ab jljklj ab

Thanks a lot.

Is it not possible to do it using grep or egrep....? :wink:
[As I need to do it using grep only....:o]

This sounds like homework. Please repost over in the homework forums.

:(..It's not a home work..but rest of all the code uses grep to find other combinations...Just wanted to add this too using grep.

gawk

$ awk -F"ab" 'NF==3'  file

perl:

open FH,"<a.txt";
while(<FH>){
  chomp;
  my @tmp =split;
  my @t = grep { $_ eq "ab" } @tmp;
  print $_,"\n" if $#t eq 1;
}