aboutsummaryrefslogtreecommitdiff
path: root/base/pr-dups
blob: 211ab64428d6560ce0447dda9996f6ecf424be25 (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
55
#!/usr/bin/env perl

# Finds duplicate adjacent words.
# http://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/

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