1745790 Members
4116 Online
108722 Solutions
New Discussion юеВ

rows to column

 
DeafFrog
Valued Contributor

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
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

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

DeafFrog
Valued Contributor

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
Dennis Handly
Acclaimed Contributor

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="," ' ....