Skip to content

Instantly share code, notes, and snippets.

@discordianfish
Created January 31, 2012 12:24
Show Gist options
  • Save discordianfish/1710261 to your computer and use it in GitHub Desktop.
Save discordianfish/1710261 to your computer and use it in GitHub Desktop.
merges multiple lines based on first field
#!/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"
@discordianfish
Copy link
Author

example input:

foo bar
foo baz
foo bla fasel
foo ranz
bla blub
bla ipsum
bla temp foobar

@discordianfish
Copy link
Author

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