Created
January 31, 2012 12:24
-
-
Save discordianfish/1710261 to your computer and use it in GitHub Desktop.
merges multiple lines based on first field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my $DELIM = shift || ' '; | |
my $last; | |
my @stash; | |
while (<STDIN>) | |
{ | |
chomp; | |
my @line = split /$DELIM/; | |
my $field = shift @line; | |
unless (defined $last and $field eq $last) | |
{ | |
print "\n" if defined $last; | |
undef $last; | |
} | |
print $last ? $DELIM : $field . $DELIM; | |
print join $DELIM, @line; | |
$last = $field; | |
} | |
print "\n" |
example output (cat uniq-j.txt | ./uniq-j.pl
):
foo bar baz bla fasel ranz
bla blub ipsum temp foobar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example input: