transpose file content with ID columns in KSH88
transpose file content with ID columns in KSH88
am 01.10.2007 13:29:03 von boston01
I have to transpose columns in a file with ID fields on it left in
KSH88. For example:
Original lines
name time gb dirname
===============================
DIRSIZE_IN_GB T0001 0.00 src
DIRSIZE_IN_GB T0001 0.05 logs
DIRSIZE_IN_GB T0001 0.00 bin
DIRSIZE_IN_GB T0002 0.02 src
DIRSIZE_IN_GB T0002 0.04 logs
DIRSIZE_IN_GB T0002 0.07 bin
transposed lines:
name time src_gb logs_gb bin_gb
=====================================
DIRSIZE_IN_GB T0001 0.00 0.05 0.00
DIRSIZE_IN_GB T0002 0.02 0.04 0.07
Thanks!
Re: transpose file content with ID columns in KSH88
am 01.10.2007 14:33:32 von Janis Papanagnou
On 1 Okt., 13:29, bosto...@gmail.com wrote:
> I have to transpose columns in a file with ID fields on it left in
> KSH88. For example:
> Original lines
> name time gb dirname
> ===============================
> DIRSIZE_IN_GB T0001 0.00 src
> DIRSIZE_IN_GB T0001 0.05 logs
> DIRSIZE_IN_GB T0001 0.00 bin
> DIRSIZE_IN_GB T0002 0.02 src
> DIRSIZE_IN_GB T0002 0.04 logs
> DIRSIZE_IN_GB T0002 0.07 bin
>
> transposed lines:
>
> name time src_gb logs_gb bin_gb
> =====================================
> DIRSIZE_IN_GB T0001 0.00 0.05 0.00
> DIRSIZE_IN_GB T0002 0.02 0.04 0.07
>
> Thanks!
If your data are as regular as shown above...
awk '
BEGIN{OFS="\t"; print "your new headers here"}
NR<=2{next}
$NF=="src"{src=$3;next}
$NF=="logs"{logs=$3;next}
$NF=="bin"{bin=$3; print $1,$2,src,logs,bin}
' orig-file >new-file
(untested program)
Janis
Re: transpose file content with ID columns in KSH88
am 01.10.2007 16:11:31 von boston01
On Oct 1, 8:33 am, Janis wrote:
> On 1 Okt., 13:29, bosto...@gmail.com wrote:
>
>
>
> > I have to transpose columns in a file with ID fields on it left in
> > KSH88. For example:
> > Original lines
> > name time gb dirname
> > ===============================
> > DIRSIZE_IN_GB T0001 0.00 src
> > DIRSIZE_IN_GB T0001 0.05 logs
> > DIRSIZE_IN_GB T0001 0.00 bin
> > DIRSIZE_IN_GB T0002 0.02 src
> > DIRSIZE_IN_GB T0002 0.04 logs
> > DIRSIZE_IN_GB T0002 0.07 bin
>
> > transposed lines:
>
> > name time src_gb logs_gb bin_gb
> > =====================================
> > DIRSIZE_IN_GB T0001 0.00 0.05 0.00
> > DIRSIZE_IN_GB T0002 0.02 0.04 0.07
>
> > Thanks!
>
> If your data are as regular as shown above...
>
> awk '
> BEGIN{OFS="\t"; print "your new headers here"}
> NR<=2{next}
> $NF=="src"{src=$3;next}
> $NF=="logs"{logs=$3;next}
> $NF=="bin"{bin=$3; print $1,$2,src,logs,bin}
> ' orig-file >new-file
>
> (untested program)
>
> Janis
Thanks Janis. I forgot the mention that the number of directories can
change. But your input is great, I will do some testing. Any idea
about using KSH88 shell script only?
John
Re: transpose file content with ID columns in KSH88
am 01.10.2007 16:49:17 von Janis Papanagnou
On 1 Okt., 16:11, bosto...@gmail.com wrote:
> On Oct 1, 8:33 am, Janis wrote:
>
>
>
> > On 1 Okt., 13:29, bosto...@gmail.com wrote:
>
> > > I have to transpose columns in a file with ID fields on it left in
> > > KSH88. For example:
> > > Original lines
> > > name time gb dirname
> > > ===============================
> > > DIRSIZE_IN_GB T0001 0.00 src
> > > DIRSIZE_IN_GB T0001 0.05 logs
> > > DIRSIZE_IN_GB T0001 0.00 bin
> > > DIRSIZE_IN_GB T0002 0.02 src
> > > DIRSIZE_IN_GB T0002 0.04 logs
> > > DIRSIZE_IN_GB T0002 0.07 bin
>
> > > transposed lines:
>
> > > name time src_gb logs_gb bin_gb
> > > =====================================
> > > DIRSIZE_IN_GB T0001 0.00 0.05 0.00
> > > DIRSIZE_IN_GB T0002 0.02 0.04 0.07
>
> > > Thanks!
>
> > If your data are as regular as shown above...
>
> > awk '
> > BEGIN{OFS="\t"; print "your new headers here"}
> > NR<=2{next}
> > $NF=="src"{src=$3;next}
> > $NF=="logs"{logs=$3;next}
> > $NF=="bin"{bin=$3; print $1,$2,src,logs,bin}
> > ' orig-file >new-file
>
> > (untested program)
>
> > Janis
>
> Thanks Janis. I forgot the mention that the number of directories can
> change.
Is there a fixed set of directories? Your headline does show only
three columns *_gb so I assumed there are just the three directories.
If there are arbitrary directories possible you have to use a generic
approach where you store/catenate the data in an associative array
with the second field as index.
If some of the three directories may be missing just clear the
variables src, logs, and bin after having printed them, by assigning a
"-" or something like that.
If the triggering /bin/ directory may not be present, then compare the
second field $2 against the previously stored field $2; if they differ
then print the line. (You also need an END clause for the final line
of output.)
> But your input is great, I will do some testing. Any idea
> about using KSH88 shell script only?
You may see above that your requirements are not quite clear. Because
ksh88 is lacking associative arrays some solutions, if possible, would
be very bulky at least. Mind that awk is *the* data processing tool
for such purposes.
Janis
> John
Re: transpose file content with ID columns in KSH88
am 01.10.2007 17:14:15 von William James
On Oct 1, 9:11 am, bosto...@gmail.com wrote:
> On Oct 1, 8:33 am, Janis wrote:
>
>
>
> > On 1 Okt., 13:29, bosto...@gmail.com wrote:
>
> > > I have to transpose columns in a file with ID fields on it left in
> > > KSH88. For example:
> > > Original lines
> > > name time gb dirname
> > > ===============================
> > > DIRSIZE_IN_GB T0001 0.00 src
> > > DIRSIZE_IN_GB T0001 0.05 logs
> > > DIRSIZE_IN_GB T0001 0.00 bin
> > > DIRSIZE_IN_GB T0002 0.02 src
> > > DIRSIZE_IN_GB T0002 0.04 logs
> > > DIRSIZE_IN_GB T0002 0.07 bin
>
> > > transposed lines:
>
> > > name time src_gb logs_gb bin_gb
> > > =====================================
> > > DIRSIZE_IN_GB T0001 0.00 0.05 0.00
> > > DIRSIZE_IN_GB T0002 0.02 0.04 0.07
>
> > > Thanks!
>
> > If your data are as regular as shown above...
>
> > awk '
> > BEGIN{OFS="\t"; print "your new headers here"}
> > NR<=2{next}
> > $NF=="src"{src=$3;next}
> > $NF=="logs"{logs=$3;next}
> > $NF=="bin"{bin=$3; print $1,$2,src,logs,bin}
> > ' orig-file >new-file
>
> > (untested program)
>
> > Janis
>
> Thanks Janis. I forgot the mention that the number of directories can
> change. But your input is great, I will do some testing. Any idea
> about using KSH88 shell script only?
Why do you want to shun awk? Do you think that it will destroy
your computer?
Re: transpose file content with ID columns in KSH88
am 01.10.2007 19:40:40 von spcecdt
In article <1191238143.263720.161520@22g2000hsm.googlegroups.com>,
wrote:
>I have to transpose columns in a file with ID fields on it left in
>KSH88. For example:
>Original lines
>name time gb dirname
>===============================
>DIRSIZE_IN_GB T0001 0.00 src
>DIRSIZE_IN_GB T0001 0.05 logs
>DIRSIZE_IN_GB T0001 0.00 bin
>DIRSIZE_IN_GB T0002 0.02 src
>DIRSIZE_IN_GB T0002 0.04 logs
>DIRSIZE_IN_GB T0002 0.07 bin
>
>
>transposed lines:
>
>name time src_gb logs_gb bin_gb
>=====================================
>DIRSIZE_IN_GB T0001 0.00 0.05 0.00
>DIRSIZE_IN_GB T0002 0.02 0.04 0.07
If the key field will always have the form of T
and there will always be a specific set of directories known beforehand,
it would be fairly straightforward in ksh88. Is that the case?
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Re: transpose file content with ID columns in KSH88
am 01.10.2007 21:26:27 von Miles
On Oct 1, 6:29 am, bosto...@gmail.com wrote:
> I have to transpose columns in a file with ID fields on it left in
> KSH88. For example:
> Original lines
> name time gb dirname
> ===============================
> DIRSIZE_IN_GB T0001 0.00 src
> DIRSIZE_IN_GB T0001 0.05 logs
> DIRSIZE_IN_GB T0001 0.00 bin
> DIRSIZE_IN_GB T0002 0.02 src
> DIRSIZE_IN_GB T0002 0.04 logs
> DIRSIZE_IN_GB T0002 0.07 bin
>
> transposed lines:
>
> name time src_gb logs_gb bin_gb
> =====================================
> DIRSIZE_IN_GB T0001 0.00 0.05 0.00
> DIRSIZE_IN_GB T0002 0.02 0.04 0.07
>
> Thanks!
Every now and then I kinda like a challenge. See if this ugly code
works:
#!/usr/bin/ksh
function print_title {
SAVE=$B
NEW=1
print "$A $B $C \c"
}
NEW=0
cat /tmp/a | while read A B C D
do
if [[ $NEW = 0 ]]
then
print_title
else
if [[ $B = $SAVE ]]
then
print "$C \c"
else
print
print_title
fi
fi
done
With:
DIRSIZE_IN_GB T0001 0.00 src
DIRSIZE_IN_GB T0001 0.05 logs
DIRSIZE_IN_GB T0001 0.00 bin
DIRSIZE_IN_GB T0002 0.02 src
DIRSIZE_IN_GB T0002 0.04 logs
DIRSIZE_IN_GB T0002 0.07 bin
Gives for output:
DIRSIZE_IN_GB T0001 0.00 0.05 0.00
DIRSIZE_IN_GB T0002 0.02 0.04 0.07
I didn't do the titles.
Miles