Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
cancel
Showing results for 
Search instead for 
Did you mean: 

rows to column

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