Skip to content

Commit

Permalink
vnl-uniq can take skip-fields < 0
Browse files Browse the repository at this point in the history
This is an extension. It allows one to skip all but the last -N fields
  • Loading branch information
dkogan committed Dec 21, 2019
1 parent 1027d6a commit 46c5fee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
21 changes: 21 additions & 0 deletions test/test_vnl-uniq.pl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@
20 2
30 3
EOF
check( <<'EOF', qw(-d -f 1), '$data1' );
# x y
2 2
10 1
20 2
30 3
EOF
check( <<'EOF', qw(-d -f-1), '$data1' );
# x y
2 2
10 1
20 2
30 3
EOF
check( <<'EOF', qw(-d -f -1), '$data1' );
# x y
2 2
10 1
20 2
30 3
EOF

# print duplicate lines, but don't look at the first column for the duplicate
# detection. Here I print ALL the duplicates; since I skipped the first column,
Expand Down
21 changes: 20 additions & 1 deletion vnl-uniq
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ The most common options are (from the GNU uniq manpage):
METHOD={none(default),prepend,separate}
-f, --skip-fields=N
avoid comparing the first N fields
avoid comparing the first N fields (vnl-uniq extension: N<0 avoids
comparing all but the LAST -N fields. To use ONLY the one last field,
pass -f -1 or --skip-fields=-1)
--group[=METHOD]
show all items, separating groups with an empty line;
Expand Down Expand Up @@ -123,6 +125,16 @@ for my $key(keys %$options)
}

my $inputs = read_and_preparse_input($filenames);

if( defined $options->{'skip-fields'} &&
$options->{'skip-fields'} < 0 )
{
# I want to use only the last -N fields, so I skip the first Nfields-N
# fields
my $Nfields = @{$inputs->[0]{keys}};
$options->{'skip-fields'} += $Nfields;
}

my $ARGV_new = reconstruct_substituted_command($inputs, $options, [], \@specs);

print '# ';
Expand Down Expand Up @@ -191,6 +203,13 @@ C<--vnl-count NAME> can be given to name the C<count> column. C<-c> is still
supported to add the default new column named C<count>, but if another name is
wanted, C<--vnl-count> does that. C<--vnl-count> implies C<-c>
=item *
In addition to the normal behavior of skipping fields at the start, C<-f> and
C<--skip-fields> can take a negative argument to skip the I<all but the last> N
fields. For instance, to use only the one last field, pass C<-f -1> or
C<--skip-fields=-1>.
=back
Past that, everything C<uniq> does is supported, so see that man page for
Expand Down

0 comments on commit 46c5fee

Please sign in to comment.