Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uniq: one input file #819

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions bin/uniq
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ License: perl

use strict;

my $VERSION = '1.0';
use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

my $VERSION = '1.1';

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

sub usage {
print "usage: $0 [-c | -d | -u] [-f fields] [-s chars] [input files]\n";
exit 1;
print "usage: $0 [-c | -d | -u] [-f fields] [-s chars] ",
"[input_file [output_file]]\n";
exit EX_FAILURE;
}

sub version { print "$0 (Perl Power Tools) $VERSION\n"; exit 0; }
Expand Down Expand Up @@ -58,12 +62,38 @@ while (@ARGV && $ARGV[0] =~ /^[-+]/) {
warn "$0: invalid option -- $_\n";
usage();
}
my $infile = shift;
my $outfile = shift;
if (@ARGV) {
warn "$0: unexpected argument: '$ARGV[0]'\n";
usage();
}
my ($fh, $out, $comp, $save_comp, $line, $save_line, $count, $eof);

my ($comp, $save_comp, $line, $save_line, $count, $eof);
if (defined $infile) {
if (-d $infile) {
warn "$0: '$infile' is a directory\n";
exit EX_FAILURE;
}
unless (open $fh, '<', $infile) {
warn "$0: failed to open '$infile': $!\n";
exit EX_FAILURE;
}
} else {
$fh = *STDIN;
}
if (defined $outfile) {
unless (open $out, '>', $outfile) {
warn "$0: failed to open '$outfile': $!\n";
exit EX_FAILURE;
}
} else {
$out = *STDOUT;
}

# prime the pump
$comp = $line = <>;
exit 0 unless defined $line;
$comp = $line = <$fh>;
exit EX_SUCCESS unless defined $line;
if ($optf) {($comp) = (split ' ', $comp, $optf+1)[$optf] }
if ($opts) { $comp = substr($comp, $opts) }

Expand All @@ -73,22 +103,22 @@ while (!$eof) {
$save_comp = $comp;
$count = 1;
DUPS:
while (!($eof = eof())) {
$comp = $line = <>;
while (!($eof = eof($fh))) {
$comp = $line = <$fh>;
if ($optf) {($comp) = (split ' ', $comp, $optf+1)[$optf] }
if ($opts) { $comp = substr($comp, $opts) }
last DUPS if $comp ne $save_comp;
++$count;
}
# when we get here, $save_line is the first occurrence of a sequence
# of duplicate lines, $count is the number of times it appears
if ($optc) { printf "%7d $save_line", $count }
elsif ($optd) { print $save_line if $count > 1 }
elsif ($optu) { print $save_line if $count == 1 }
else { print $save_line }
if ($optc) { printf {$out} '%7d %s', $count, $save_line }
elsif ($optd) { print {$out} $save_line if $count > 1 }
elsif ($optu) { print {$out} $save_line if $count == 1 }
else { print {$out} $save_line }
}

exit 0;
exit EX_SUCCESS;

__END__

Expand All @@ -98,7 +128,8 @@ uniq - report or filter out repeated lines in a file

=head1 SYNOPSIS

uniq [B<-c> | B<-d> | B<-u>] [B<-f> I<fields>] [B<-s> I<chars>] [I<input files>]
uniq [B<-c> | B<-d> | B<-u>] [B<-f> I<fields>] [B<-s> I<chars>]
[input_file [output_file]]

=head1 DESCRIPTION

Expand Down
Loading