Skip to content

Commit f9f8b9f

Browse files
authored
uniq: one input file (#819)
* uniq: one input file * Standards document explains uniq has two optional file arguments, input and output * GNU and BSD versions follow this; do the same and stop treating subsequent file arguments as extra input files * test1: printf "a\na\nb\nc\nc\n" | perl uniq ---> implicit stdin and stdout * test2: perl uniq a.c ---> read a.c, write to stdout * test3: perl uniq a.c b.c ---> read a.b, write to b.c * test4: perl uniq A B C ---> invalid, too many args * wrong varname in error message
1 parent cee2fd5 commit f9f8b9f

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

bin/uniq

+45-14
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ License: perl
1616

1717
use strict;
1818

19-
my $VERSION = '1.0';
19+
use constant EX_SUCCESS => 0;
20+
use constant EX_FAILURE => 1;
21+
22+
my $VERSION = '1.1';
2023

2124
END {
2225
close STDOUT || die "$0: can't close stdout: $!\n";
2326
$? = 1 if $? == 255; # from die
2427
}
2528

2629
sub usage {
27-
print "usage: $0 [-c | -d | -u] [-f fields] [-s chars] [input files]\n";
28-
exit 1;
30+
print "usage: $0 [-c | -d | -u] [-f fields] [-s chars] ",
31+
"[input_file [output_file]]\n";
32+
exit EX_FAILURE;
2933
}
3034

3135
sub version { print "$0 (Perl Power Tools) $VERSION\n"; exit 0; }
@@ -58,12 +62,38 @@ while (@ARGV && $ARGV[0] =~ /^[-+]/) {
5862
warn "$0: invalid option -- $_\n";
5963
usage();
6064
}
65+
my $infile = shift;
66+
my $outfile = shift;
67+
if (@ARGV) {
68+
warn "$0: unexpected argument: '$ARGV[0]'\n";
69+
usage();
70+
}
71+
my ($fh, $out, $comp, $save_comp, $line, $save_line, $count, $eof);
6172

62-
my ($comp, $save_comp, $line, $save_line, $count, $eof);
73+
if (defined $infile) {
74+
if (-d $infile) {
75+
warn "$0: '$infile' is a directory\n";
76+
exit EX_FAILURE;
77+
}
78+
unless (open $fh, '<', $infile) {
79+
warn "$0: failed to open '$infile': $!\n";
80+
exit EX_FAILURE;
81+
}
82+
} else {
83+
$fh = *STDIN;
84+
}
85+
if (defined $outfile) {
86+
unless (open $out, '>', $outfile) {
87+
warn "$0: failed to open '$outfile': $!\n";
88+
exit EX_FAILURE;
89+
}
90+
} else {
91+
$out = *STDOUT;
92+
}
6393

6494
# prime the pump
65-
$comp = $line = <>;
66-
exit 0 unless defined $line;
95+
$comp = $line = <$fh>;
96+
exit EX_SUCCESS unless defined $line;
6797
if ($optf) {($comp) = (split ' ', $comp, $optf+1)[$optf] }
6898
if ($opts) { $comp = substr($comp, $opts) }
6999

@@ -73,22 +103,22 @@ while (!$eof) {
73103
$save_comp = $comp;
74104
$count = 1;
75105
DUPS:
76-
while (!($eof = eof())) {
77-
$comp = $line = <>;
106+
while (!($eof = eof($fh))) {
107+
$comp = $line = <$fh>;
78108
if ($optf) {($comp) = (split ' ', $comp, $optf+1)[$optf] }
79109
if ($opts) { $comp = substr($comp, $opts) }
80110
last DUPS if $comp ne $save_comp;
81111
++$count;
82112
}
83113
# when we get here, $save_line is the first occurrence of a sequence
84114
# of duplicate lines, $count is the number of times it appears
85-
if ($optc) { printf "%7d $save_line", $count }
86-
elsif ($optd) { print $save_line if $count > 1 }
87-
elsif ($optu) { print $save_line if $count == 1 }
88-
else { print $save_line }
115+
if ($optc) { printf {$out} '%7d %s', $count, $save_line }
116+
elsif ($optd) { print {$out} $save_line if $count > 1 }
117+
elsif ($optu) { print {$out} $save_line if $count == 1 }
118+
else { print {$out} $save_line }
89119
}
90120

91-
exit 0;
121+
exit EX_SUCCESS;
92122

93123
__END__
94124
@@ -98,7 +128,8 @@ uniq - report or filter out repeated lines in a file
98128
99129
=head1 SYNOPSIS
100130
101-
uniq [B<-c> | B<-d> | B<-u>] [B<-f> I<fields>] [B<-s> I<chars>] [I<input files>]
131+
uniq [B<-c> | B<-d> | B<-u>] [B<-f> I<fields>] [B<-s> I<chars>]
132+
[input_file [output_file]]
102133
103134
=head1 DESCRIPTION
104135

0 commit comments

Comments
 (0)