2010-11-24 29 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