Convert rows to columns based on key and count

Team,
I am having requirement to convert rows to columns
Input is:

key ,count, id1, pulse1, id2, pulse2 ,id3, pulse3 
12, 2 , 14 , 56 , 15, 65
13, 3, 12, 32, 14, 23, 18, 54
22, 1 , 32, 42

Expected Out put:

key, id,pulse
12, 14, 56
12, 15, 65
13 ,12, 32
13, 14 ,23
13, 18 ,54
22 ,32, 42

Explanation: based on "count" , id and pulse sets will be repeated "count" number of times
I need a O/p which should contain key,id,pulse in a normalized was as shown above.your help is appreciated

Have a look at the bottom of this page, where you will find half a dozen threads dealing with exactly this. "Convert Rows to Columns" has been asked and answered so often we could fill a separate board with it.

Against popular belief it is NOT FORBIDDEN to use the search function!

I hope this helps.

bakunin

I checked the following links, but I was not successful in converting rows to columns dynamically based on count. I am not very familiar with unix scripting ,I usually pick the unix commands through forums.

Wouldn't it be nice to develop some personal skills?
Anyhow, try

awk '
NR == 1 {FS = OFS = ","
         print "key, id,pulse"
         next
        }
        {gsub(/ ,/,",")
         for (i=3; i<=NF; i+=2) print $1, $i, $(i+1)
        }
' file
key, id,pulse
12, 14, 56
12, 15, 65
13, 12, 32
13, 14, 23
13, 18, 54
22, 32, 42

As the commas in both sample input and output seem randomly seeded, I took the freedom to slightly align them...

1 Like