rows to column (267 Views)
Reply
Valued Contributor
DeafFrog
Posts: 294
Registered: ‎02-25-2009
Message 1 of 4 (267 Views)

rows to column

hi Gurus ,

I have a file with entries as below:

var1:mango
quantity:w1245
season: alphanso
var1:apple
quantity:c324
season:winter
var1:cherry
quantity:b4326
season:autumn
....
......

I want the file o/p like

var1 quantity season

mango w1245 alphanso
apple c324 winter
cherry b4326 autumn
mango b67588 spring
....
...

Many thanks ,
FrogIsDeaf
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,958
Registered: ‎03-06-2006
Message 2 of 4 (256 Views)

Re: rows to column

Assuming you have no spaces in the file and the three lines are always in that order:

awk -F:  '
{
var1 = $2
getline
quantity = $2
getline
season = $2
print var1, quantity, season
}' input-file

Please use plain text.
Valued Contributor
DeafFrog
Posts: 294
Registered: ‎02-25-2009
Message 3 of 4 (215 Views)

Re: rows to column

Hi Dennis ,

 

thank you for the reply.

I used awk -F':' and extracted the second filed and then did a #paste -d"," - - -  < details.txt , to generate the required csv.

 

Thanks once again.

 

 

FrogIsDeaf
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,958
Registered: ‎03-06-2006
Message 4 of 4 (211 Views)

Re: rows to column

>I used awk -F':' and extracted the second field and then did a paste

 

You didn't need to use both awk and paste, you can do it all in awk by changing OFS to comma:

awk -F:  -v OFS="," ' ....

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation