Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (8)
    As of 2014-10-02 18:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (72 votes), past polls