Re: AWK or otherway to convert transpose Columns to Rows
From: John L (jl_at_lammtarra.notthisbit.fslife.co.uk)
Date: 06/29/05
- Next message: HellZFury_at_gmail.com: "BASH help please??"
- Previous message: Ralph A. Moeritz: "how to write portable shell scripts?"
- In reply to: Akhwashah: "AWK or otherway to convert transpose Columns to Rows"
- Next in thread: Bruce Barnett: "Re: AWK or otherway to convert transpose Columns to Rows"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Jun 2005 08:59:48 +0100
"Akhwashah" <akhwashah.@WARMmail.com> wrote in message news:d9tgv7$u9b$1@otis.netspace.net.au...
> HI All,
>
>
> I have a data file with roughly the following format :
>
> AAA,BBB,CCC,DDD,1234,5678,555,678....999
>
> basically a list of text fields, with several columns of data by month
>
> Which I would like converted to :
>
> AAA,BBB,CCC,DDD,JAN,1234
> AAA,BBB,CCC,DDD,FEB,5678
> AAA,BBB,CCC,DDD,MAR,555
> AAA,BBB,CCC,DDD,MAR,678
> :
> AAA,BBB,CCC,DDD,DEC,999
>
> I have been able to get it happening very manually by using multiple print
> statments in an awk program file, but I would like to make it more dynamic,
> as the columns may represent different months at any point in time, so i was
> hoping to somehow provide an argument. ie data could be for JAN-DEC or
> AUG-DEC..
>
There is an awk newsgroup, news:comp.lang.awk
#!/bin/sh
# process options and arguments, for now we'll just do:
FIRSTMONTH=$1
# and check that this is a number between 1 and 12
awk -vFIRST=${FIRSTMONTH} '
# sets awk variable FIRST from shell variable $FIRSTMONTH
BEGIN {
FS = OFS = ","
split("Jan Feb Mar Apr May Jun ...", Monthnames, " ")
}
{
for (i = 1; i <= 12; i++)
{
month = i - 1 + FIRST
print $1, $2, $3, $4, Monthnames[month], $(i + 4)
}
}
'
and you can see how this can be extended if the last month
also varies.
-- John.
- Next message: HellZFury_at_gmail.com: "BASH help please??"
- Previous message: Ralph A. Moeritz: "how to write portable shell scripts?"
- In reply to: Akhwashah: "AWK or otherway to convert transpose Columns to Rows"
- Next in thread: Bruce Barnett: "Re: AWK or otherway to convert transpose Columns to Rows"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|