summaryrefslogtreecommitdiffstats
path: root/.local/bin/dups
blob: eb450f5b2e507c071e333ed1fc174ad7e2d04b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env perl

# Finds duplicate adjacent words.

use strict ;

my $DupCount = 0 ;

if (!@ARGV) {
  print "usage: dups <file> ...\n" ;
  exit ;
}

while (1) {
  my $FileName = shift @ARGV ;

  # Exit code = number of duplicates found.
  exit $DupCount if (!$FileName) ;

  open FILE, $FileName or die $!;

  my $LastWord = "" ;
  my $LineNum = 0 ;

  while (<FILE>) {
    chomp ;

    $LineNum ++ ;

    my @words = split (/(\W+)/) ;

    foreach my $word (@words) {
      # Skip spaces:
      next if $word =~ /^\s*$/ ;

      # Skip punctuation:
      if ($word =~ /^\W+$/) {
        $LastWord = "" ;
        next ;
      }

      # Found a dup?
      if (lc($word) eq lc($LastWord)) {
        print "$FileName:$LineNum $word\n" ;
        $DupCount ++ ;
      } # Thanks to Sean Cronin for tip on case.

      # Mark this as the last word:
      $LastWord = $word ;
    }
  }

  close FILE ;
}