Dallos Flugël Dallos Flugël - 2 months ago 8
Linux Question

What is wrong in this grep command?

Okay... so I'm trying to make a dictionary using Crunch and Grep I've also tried using Perl instead.

crunch 8 12 1234567890 -d 2@ | grep -v '\([0-9]\) .*\1.*\1.*\1.*' | grep 41106041


So, basically i want to filter all passwords which numbers appear 3 times

grep 41106041


Is just to test if the code works.. and it doesn't.

I've also tried some "C style" perl code as I'm still a newbie to perl:

#! /bin/perl
@lines=<STDIN>;
$c=0;
foreach $number(@lines)
{
$acum=undef;
$pos=0;
while($pos <= &countdig($number))
{
if ($acum=$digit)
{
$c=$c+1;
}
else
{
$c=0;
}
$acum=$digit;

}
if ($c=3)
{
print "$number"." ";
}
$c=0;
}

sub countdig
{
my($j)=0;
chomp(my(@n)=@_);
print "first dig $n[$j] \n";
while($_[0][$j]>=0 && $_[0][j]<=9)
{
$j+=1;

}
print "j total : $j \n";

$j;
}


Countdig is supposed to count the number of digits but thing is.. I can't access a scalar variable as a list.. so well if you guys could explain me how to make it work it would be very appreciated.

Answer

Your grep is failing because you are searching for 4 identical digits.

$ printf '12345678\n41106041\n87654321\n' | grep -v '\(.\).*\1.*\1'
12345678
87654321

The problem is not being a newbie to Perl, cause it's easy to turn a C solution

// For each line

char *num = ...;

int digits[10];
for (int i=0; i<10; ++i) {
   digits[i] = 0;
}

const char *p = num;
int triple = 0;
for (; *p && *p != '\n'; ++p) {
   if (++digits[*p - '0'] == 3) {
      triple = 1;
      break;
   }
}

if (triple) {
   ...
}

into a Perl solution

while (my $num = <>) {
   chomp($num);

   my @digits;
   my $triple;
   for my $digit (split //, $num) {
      if (++$digits[$digit] == 3) {
         $triple = 1;
         last;
      }
   }

   say $num if $triple;
}

Of course, a Perl programmer might very well use the same approach you used for grep.

while (<>) {
   if (!/(.).*\1.*\1/) {
      print;
   }
}
Comments