Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl use strict; use Getopt::Long; use Pod::Usage; my ( $delim, $colspec, $last, $man, $help ) = ( 'default', '1', 0, 0, +0 ); my $argok = GetOptions( 'l' => \$last, 'c=s' => \$colspec, 'd=s' => \$delim, 'm' => \$man, 'h|?' => \$hel +p ); pod2usage(-exitstatus => 0, -verbose => 2) if $man; pod2usage(1) unless ( $argok and $colspec =~ /^\d[\d,]*$/ and !$help a +nd ( @ARGV or !-t )); my @cols = map { $_ -1 } split( /,/, $colspec ); if ( $delim ne 'default' ) { my %ctrl = ( tab => "\t", dot => '\.', vb => '\|', bs => '\\\\' ); $delim = ( exists( $ctrl{$delim} )) ? qr{$ctrl{$delim}} : qr{$deli +m}; } my @lastseen; my $heldline = ''; while (<>) { my @tkns = ( $delim eq 'default' ) ? split : split $delim; my $match = 0; for my $i ( 0 .. $#cols ) { $match++ if ( $tkns[$cols[$i]] eq $lastseen[$i] ); } if ( @cols == $match ) { $heldline = $_ if ( $last ); } else { print $heldline; $heldline = $_; $lastseen[$_] = $tkns[$cols[$_]] for ( 0..$#cols ); } } print $heldline; =head1 NAME col-uniq =head1 SYNOPSIS col-uniq [-l] [-d delim] [-c col#[,col#...]] [sorted.list ...] col-uniq -m # to print user manual =head1 DESCRIPTION This tool will scan through lines of text that have been sorted with respect to one or more selected columns, and in the event that two or more consecutive lines have the same value(s) in the selected column(s), only one line from the matching set will be output. All other lines, having non-repeating values in the selected column(s), are also printed. This tool will only work as intended if the input has been sorted with respect to the column(s) of interest, so a typical usage would be: sort [...] some.list | col-uniq [...] By default, the first whitespace-delimited column will be taken as the column of interest, and for every set of two or more consecutive lines having the same value in that column, only the first line will be printed to STDOUT (along with all the other "unique" lines). Use the "-c col#[,...]" option to select one or more specific columns of interest, by index number (first column on each line is "1"). For example, in a directory where varying numbers of files are created per day, this command line: ls -lt | col-uniq -c 6,7 will show only the first file created on each date. (The "ls" options "-lt" produce a full-detail file list, sorted by date.) Use the "-d delim_regex" option to select something other than whitespace as the delimiter for splitting each line into columns. The given string is passed to the perl "split" function as a regular expression, so a wide assortment of column separation strategies is possible (but bear in mind that the input must be sorted on the basis of the designated columns, in order to locate all duplicates). Also, some "specially defined" split expressions are provided for convenience: -d tab : split on tabs only (not other whitespace) dot : split on period characters vb : split on the vertical-bar (pipe) symbol "|" bs : split on backslash Note that if you set the delimiter to a pattern that never occurs in the data, the result will be to check for (and remove) consecutive full-line duplications. (This will include sets of blank lines.) The "-l" option can be used to preserve only the last line from each set of matching lines (in case that is preferable to keeping only the first). =head1 AUTHOR David Graff =cut

In reply to col-uniq -- remove lines that match on selected column(s) by graff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found