0

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?

  • 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
2

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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