2010-11-24 74 views
1

我有一个CSS元素的文件,我试图检查任何重复的CSS元素,然后输出显示重复行的行。发现重复行

 
    ###Test 
    ###ABC 
    ###test 
    ##.hello 
    ##.ABC 
    ##.test 
    bob.com###Test 
    ~qwerty.com###Test 
    ~more.com##.ABC 

###Test & ##.ABC已经存在于列表中,我想办法输出在文件中使用的线,基本上重复检查(区分大小写)。所以使用上面的列表,我会生成这样的东西..

 
    Line 1: ###Test 
    Line 7: bob.com###Test 
    Line 8: ~qwerty.com###Test 

    Line 5: ##.ABC 
    Line 9: ~more.com##.ABC 

东西在bash,或者也许是perl?

谢谢:)

回答

1

我已经被你的问题的挑战,所以我写了你的脚本。希望你喜欢它。 :)

#!/usr/bin/perl 

use strict; 
use warnings; 

sub loadf($); 

{ 
    my @file = loadf("style.css"); 
    my @inner = @file; 
    my $l0 = 0; my $l1 = 0; my $l2 = 0; my $dc = 0; my $tc; 
    foreach my $line (@file) { 
     $l1++; 
     $line =~ s/^\s+//; 
     $line =~ s/\s+$//; 
     foreach my $iline (@inner) { 
      $l2++; 
      $iline =~ s/^\s+//; 
      $iline =~ s/\s+$//; 
      next if ($iline eq $line); 
      if ($iline =~ /\b$line\b/) { 
       $dc++; 
       if ($dc > 0) { 
        if ($l0 == 0) { 
         print "Line " . $l1 . ": " . $line . "\n"; 
         $l0++; 
        } 
        print "Line " . $l2 . ": " . $iline . "\n"; 
       } 
      } 
     } 
     print "\n" unless($dc == 0); 
     $dc = 0; $l0 = 0; $l2 = 0; 
    } 
} 

sub loadf($) { 
    my @file = (); 
    open(FILE, $_[0] . "\n") or die("Couldn't Open " . $_[0] . "\n"); 
    @file = <FILE>; 
    close(FILE); 
    return @file; 
} 

__END__ 

这正是你所需要的。对不起,如果有点混乱。

+0

与剧本有太多的问题,需要检查线路的末端,所以1781线:##。test 第1782行:##。test-leaderboard 第1787行:##。abc 第1788行:##。abcng由于##。adc与##。abcng不同,所以不能使用##。abcng – user349418 2010-11-25 03:34:21

+0

@ user349418编辑过,再试一次。 – Ruel 2010-11-25 03:56:49

1

这似乎工作:

sort -t '#' -k 2 inputfile 

这组他们在部分#字符后:

##.ABC 
~more.com##.ABC 
###ABC 
##.hello 
##.test 
###test 
bob.com###Test 
~qwerty.com###Test 
###Test 

如果你只想看到独特的价值观:

sort -t '#' -k 2 -u inputfile 

结果:

##.ABC 
###ABC 
##.hello 
##.test 
###test 
###Test 

这个漂亮的密切复制到了问题的输出例子(它依赖于一些可能GNU特定功能):

cat -n inputfile | 
    sed 's/^ *\([0-9]\)/Line \1:/' | 
    sort -t '#' -k 2 | 
    awk -F '#+' '{if (! seen[$2]) { \ 
     if (count > 1) printf "%s\n", lines; \ 
     count = 0; \ 
     lines = "" \ 
    }; \ 
    seen[$2] = 1; \ 
    lines = lines "\n" $0; ++count} 
    END {if (count > 1) print lines}' 

结果:

Line 5: ##.ABC 
Line 9: ~more.com##.ABC 

Line 1: ###Test 
Line 7: bob.com###Test 
Line 8: ~qwerty.com###Test 
+0

我想你可能在排序的第一个例子中有一个错字。是否可以输入`sort -t'#'-k 2 inputfile`(不带-u)? – martineno 2010-11-25 04:26:51

0

下面是做到这一点的一种方式,如果需要的话,这很容易扩展到多个文件。

有了这个文件find_dups.pl

use warnings; 
use strict; 

my @lines; 
while (<>) {          # read input lines 
    s/^\s+//; s/\s+$//;       # trim whitespace 
    push @lines, {data => $_, line => $.} if $_ # store useful data 
} 

@lines = sort {length $$a{data} <=> length $$b{data}} @lines; # shortest first 

while (@lines) { 
    my ($line, @found) = shift @lines; 
    my $re = qr/\Q$$line{data}\E$/;    # search token 
    @lines = grep {        # extract matches from @lines 
     not $$_{data} =~ $re && push @found, $_ 
    } @lines; 
    if (@found) {        # write the report 
     print "line $$_{line}: $$_{data}\n" for $line, @found; 
     print "\n"; 
    } 
} 

然后perl find_dups.pl input.css打印:

 
line 5: ##.ABC 
line 9: ~more.com##.ABC 

line 1: ###Test 
line 7: bob.com###Test 
line 8: ~qwerty.com###Test