Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to reorder a file with a batch from this schema:

Default Web Site;anonymousAuthentication    
Default Web Site;windowsAuthentication
Daisy;anonymousAuthentication
Daisy;windowsAuthentication

to this schema:

Default Web Site;anonymousAuthentication;windowsAuthentication
Daisy;anonymousAuthentication;windowsAuthentication

How can I do it?

share|improve this question
    
Welcome to the site! Please check out our code formatting with the {} button. Makes it easier to read. – slhck Jan 27 '12 at 18:11

Here's your data

$ cat  t13.dat
Default Web Site;anonymousAuthentication

Default Web Site;windowsAuthentication

Daisy;anonymousAuthentication

Daisy;windowsAuthentication

Here's how to generate your result

$ perl t13.pl t13.dat
Daisy;anonymousAuthentication;windowsAuthentication
Default Web Site;anonymousAuthentication;windowsAuthentication

Here's the script

$ cat t13.pl
#!/usr/bin/perl
use strict;
use warnings;

my %values;

while (<>) {
  chomp;
  next if /^\s*$/; # ignore empty lines
  my ($category, $value) = split /;/, $_, 2;
  $values{$category} .= ';'.$value;
}

for my $category (sort keys %values) {
  print "$category$values{$category}\n";
}

You seem to be using Windows, install Perl first.


If your data doesn't contain blank lines and you are ok with slightly more cryptic solutions:

$ perl -lanF\; -e'$v{$F[0]}.=";".$F[1]; END{print "$_$v{$_}" for keys %v}' t13.dat
Daisy;anonymousAuthentication;windowsAuthentication
Default Web Site;anonymousAuthentication;windowsAuthentication
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .