diff options
Diffstat (limited to '.local/bin/dups')
-rwxr-xr-x | .local/bin/dups | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/.local/bin/dups b/.local/bin/dups new file mode 100755 index 0000000..eb450f5 --- /dev/null +++ b/.local/bin/dups | |||
@@ -0,0 +1,54 @@ | |||
1 | #!/usr/bin/env perl | ||
2 | |||
3 | # Finds duplicate adjacent words. | ||
4 | |||
5 | use strict ; | ||
6 | |||
7 | my $DupCount = 0 ; | ||
8 | |||
9 | if (!@ARGV) { | ||
10 | print "usage: dups <file> ...\n" ; | ||
11 | exit ; | ||
12 | } | ||
13 | |||
14 | while (1) { | ||
15 | my $FileName = shift @ARGV ; | ||
16 | |||
17 | # Exit code = number of duplicates found. | ||
18 | exit $DupCount if (!$FileName) ; | ||
19 | |||
20 | open FILE, $FileName or die $!; | ||
21 | |||
22 | my $LastWord = "" ; | ||
23 | my $LineNum = 0 ; | ||
24 | |||
25 | while (<FILE>) { | ||
26 | chomp ; | ||
27 | |||
28 | $LineNum ++ ; | ||
29 | |||
30 | my @words = split (/(\W+)/) ; | ||
31 | |||
32 | foreach my $word (@words) { | ||
33 | # Skip spaces: | ||
34 | next if $word =~ /^\s*$/ ; | ||
35 | |||
36 | # Skip punctuation: | ||
37 | if ($word =~ /^\W+$/) { | ||
38 | $LastWord = "" ; | ||
39 | next ; | ||
40 | } | ||
41 | |||
42 | # Found a dup? | ||
43 | if (lc($word) eq lc($LastWord)) { | ||
44 | print "$FileName:$LineNum $word\n" ; | ||
45 | $DupCount ++ ; | ||
46 | } # Thanks to Sean Cronin for tip on case. | ||
47 | |||
48 | # Mark this as the last word: | ||
49 | $LastWord = $word ; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | close FILE ; | ||
54 | } | ||