这个grep命令有什么问题?

时间:2016-10-01 02:22:35

标签: linux perl grep

好的......所以我正在尝试使用 Crunch Grep 创建一个字典。我也尝试使用 Perl

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

所以,基本上我想过滤所有数字出现3次的密码

grep 41106041 

只是为了测试代码是否有效......但事实并非如此。

我还尝试了一些“C风格”perl代码,因为我还是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应该计算数字的数量,但事情是..我不能将标量变量作为列表访问..如果你们能解释我如何让它工作,那将非常感激。< / p>

2 个答案:

答案 0 :(得分:3)

您的grep失败了,因为您的模式中存在一个杂散空间,并且您正在搜索4个相同的数字(捕获的数字,再加上反向引用的数字)。

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

问题不是Perl的新手,因为它很容易转为C解决方案

// 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) {
   ...
}

进入Perl解决方案

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;
}

当然,Perl程序员可能会使用与grep相同的方法。

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

答案 1 :(得分:0)

## using R default contrasts options
#$contrasts
#        unordered           ordered 
#"contr.treatment"      "contr.poly" 

coef(glm(b ~ a, data = test_mx, family = "binomial"))
#(Intercept)       aTRUE 
#  -24.56607    49.13214 

options(contrasts = c("contr.SAS", "contr.poly"))
coef(glm(b ~ a, data = test_mx, family = "binomial"))
#(Intercept)      aFALSE 
#   24.56607   -49.13214 
相关问题