Batch transpose a comma separated file

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?

Michele

Posted 2012-01-27T16:22:05.197

Reputation: 1

Welcome to the site! Please check out our code formatting with the {} button. Makes it easier to read. – slhck – 2012-01-27T18:11:23.903

Answers

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

RedGrittyBrick

Posted 2012-01-27T16:22:05.197

Reputation: 70 632