46019
November 28, 2009, 6:01am
1
Hi I have an input file and I want to transpose it but I need to take care that if any field is missing for a record it should be popoulated with space for that field - using a shell script
INFILE
----------
emp=1
sal=2
loc=abc
emp=2
sal=21
sal=22
loc=xyz
emp=5
loc=abc
OUTFILE
----------
emp,sal,loc
1,2,abc
2,21,
,22,xyz
5, ,abc
It is like I will have data for atleast one of the three (emp, sal, loc) but while transposing I need to see if any value is missing then (space) " " is populated.
Is emp always present or it may be missing too?
rdcwayx
November 28, 2009, 7:39am
3
on line 4 of output, emp is missing.
Yep,
I missed that.
Thank you!
46019
November 28, 2009, 8:37am
5
No, emp will not be present always. But atleast one of them (emp, sal, loc) will be present.
awk -F= 'BEGIN {
k["emp"] = 1; k["sal"] = 2; k["loc"] = 3
print "emp,sal,loc"
}
k[$1] <= k[p] {
for (i=1; i<=3; i++)
printf "%s", (v ? v : " ") \
(i == 3 ? RS : OFS)
split(x, v)
}
{ v[k[$1]] = $2; p = $1 }
END {
if (k[p] >= 3)
for (i=1; i<=3; i++)
printf "%s", (v ? v : " ") \
(i == 3 ? RS : OFS)
}' OFS=, infile
---------- Post updated at 04:19 PM ---------- Previous update was at 04:01 PM ----------
Or less noisy:
awk -F= 'BEGIN {
k["emp"] = 1; k["sal"] = 2; k["loc"] = 3
print "emp,sal,loc"; cols = 3
}
k[$1] <= k[p] { print_row(cols); split(x, v) }
{ v[k[$1]] = $2; p = $1 }
END { k[p] >= 3 && print_row(cols) }
func print_row(n) {
for (i=1; i<=n; i++)
printf "%s", (v ? v : " ") \
(i == n ? RS : OFS)
}' OFS=, infile
Bluethunder... It looks like you've added a new problem to this post. You really should start a new thread to prevent confusion.
---------- Post updated at 10:33 AM ---------- Previous update was at 10:29 AM ----------
46019:
#!/usr/bin/perl
use strict;
my @a_empdata;
my %h_empdata;
my $esl;
my $val;
my $line;
my $i;
open EMPFILE, "<esl.dat"
or die "can't open file: $!";
@a_empdata = <EMPFILE>;
close EMPFILE
or die "can't close file: $!";
print "emp\tsal\tloc\n";
$i = 0;
while ($a_empdata[$i])
{
$line = $a_empdata[$i];
chomp($line);
$esl = (split '=', $line) [0];
$val = (split '=', $line) [1];
if ( $esl eq "emp" )
{
$h_empdata{'emp'} = $val;
if ( $a_empdata[$i+1] =~ m/emp/ )
{
$h_empdata{'sal'} = " ";
$h_empdata{'loc'} = " ";
$i++;
}
elsif ( $a_empdata[$i+1] =~ m/loc/ )
{
$h_empdata{'sal'} = " ";
$h_empdata{'loc'} = (split '=', $a_empdata[$i+1]) [1];
chomp($h_empdata{'loc'});
$i = $i + 2;
}
elsif ( $a_empdata[$i+1] =~ m/sal/ )
{
$h_empdata{'sal'} = (split '=', $a_empdata[$i+1]) [1];
chomp($h_empdata{'sal'});
if ( $a_empdata[$i+2] =~ m/loc/ )
{
$h_empdata{'loc'} = (split '=', $a_empdata[$i+2]) [1];
chomp($h_empdata{'loc'});
$i = $i + 3;
}
else
{
$h_empdata{'loc'} = " ";
$i = $i + 2;
}
}
}
elsif ( $esl eq "sal" )
{
$h_empdata{'sal'} = $val;
$h_empdata{'emp'} = " ";
if ( $a_empdata[$i+1] =~ m/loc/ )
{
$h_empdata{'loc'} = (split '=', $a_empdata[$i+1]) [1];
chomp($h_empdata{'loc'});
$i = $i + 2;
}
else
{
$h_empdata{'loc'} = " ";
$i++;
}
}
elsif ( $esl eq "loc" )
{
$h_empdata{'loc'} = $val;
$h_empdata{'emp'} = " ";
$h_empdata{'sal'} = " ";
$i++;
}
print "$h_empdata{'emp'}\t$h_empdata{'sal'}\t$h_empdata{'loc'}\n";
undef %h_empdata;
}
I used tabs in the print to format the output.
./esl.pl
emp sal loc
1 2 abc
2 21
22 xyz
5 abc
The question is similar, so I left it in this thread. But indeed,
as others may reply later it's a bit confusing.
I'll move that post in a separate thread.
Thanks radoulov...much appreciated ...
hope i can get some answers to my questions..its been haunting me for the last couple of days.
cheers ~ V
I've just split the thread, you'll find the second question here .
Yes I saw that ...thats why i thanked you
---------- Post updated at 03:27 PM ---------- Previous update was at 11:52 AM ----------
Sorry about the confusion jsmithstl.
I have mananged to modify your script to input my data :D. It would be gr8 if you can add 4th column input to your script.
Thanks again.