Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.