Columns to rows

Hi,
I have a big file, with thousands of rows, and I want to put every 7 rows in a line. Input file:

str1, val2, val3
str2, val4, val5
str3, val22, val33 
str4, val44, val55
str5, val6, val7
str6, val77, val88
str7, val99, val00
str1, som2, som3
str2, som4, som5
str3, som22, som33 
str4, som44, som55
str5, som6, som7
str6, som77, som88
str7, som99, som00
str1, haha2, haha3
str2, haha4, haha5
str3, haha22, haha33 
str4, haha44, haha55
str5, haha6, haha7
str6, haha77, haha88
str7, haha99, haha00

First string it is always one and the same for the all 7 lines. And output should be like that:

str1,val2,val3,str2,val4,val5,str3,val22,val33,str4,val44,val55,str5,val6,val7,str6,val77,val88,str7,val99,val00
str1,som2,som3,str2,som4,som5,str3,som22,som33,str4,som44,som55,str5,som6,som7,str6,som77,som88,str7,som99,som00
str1,haha2,haha3,str2,haha4,haha5,str3,haha22,haha33,str4,haha44,haha55,str5,haha6,haha7,str6,haha77,haha88,str7,haha99,haha00

Thanks!

Try:

awk '{gsub(/\n|, /, ","); gsub(/,$/,"");$0=RS $0} NR>1' RS='str1' infile

Try:

sed 'N;N;N;N;N;N;s/\n/,/g; s/, /,/g' file

OK, that seems to be nice. But how can I figure out the new line, because it displays everything in a row now.
Thanks a lot!

I am not sure what you mean. With your input I get:

$ sed 'N;N;N;N;N;N;s/\n/,/g; s/, /,/g' file
str1,val2,val3,str2,val4,val5,str3,val22,val33 ,str4,val44,val55,str5,val6,val7,str6,val77,val88,str7,val99,val00
str1,som2,som3,str2,som4,som5,str3,som22,som33 ,str4,som44,som55,str5,som6,som7,str6,som77,som88,str7,som99,som00
str1,haha2,haha3,str2,haha4,haha5,str3,haha22,haha33 ,str4,haha44,haha55,str5,haha6,haha7,str6,haha77,haha88,str7,haha99,haha00

What do you get?
Perhaps this works better?

sed 'N;N;N;N;N;N;s/\n/,/g; s/ *, */,/g' file

It is not working properly. It displays strange things. E.g.

str1,val2,val3,str2,val4,val5,str3,val22,val33 ,str4,val44,val55,str5,val6,val7,str6,val77,val88,str7,val99,val00,ex

red = missing
blue = additional
Something like that, but in many different ways.

Perhaps your file is in DOS format you could try:

awk '{gsub(/ *[\r\n]+|, /, ","); gsub(/,$/,"");$0=RS $0} NR>1' RS='str1' infile

I receive on every row 28 strings and the row is ending without comma.
Like that:

str1,val2,val3,str2,val4,val5,str3,val22,val33,str4,val44,val55,str5,val6,val7,str6,val77,val88,str7,val99,val00,str1,som2,som3,str2,som4,som5,str3
som22,som33,str4,som44,som55,str5,som6,som7,str6,som77,som88,str7,som99,som00,str1,haha2,haha3,str2,haha4,haha5,str3,haha22,haha33,str4,haha44,haha55,str5,haha6....
..............
som22,som33,str4,som44,som55,str5,som6,som7,str6,som77,som88,str7,som99,som00,str1,haha2,haha3,str2,haha4,haha5,str3,haha22,haha33,str4,haha44,haha55,str5,haha6....

Is it possible, because some of the strings are empty, but not the first one. Or some format sh*t? these in red are supposed to be on a new line.
Thanks!

There is something about this input file we are not seeing in your pasted example, could you attach the testing file you are using (you might have to change it's extension to .txt for the attach file function)

1 Like

Tried it, the same thing. Always starts with str1 and then a couple of strings, ending the line without comma...like that:

str1,str1,val3,str2,val4,val5,str3,val22
str1,val33,str4,val44,val55,str5,val6,val7,str6
str1,val77,val88,str7,val99,val00,str1,som2,som3,str2,som4,som5,str3
..............
str1,str1,val3,str2,val4,val5,str3,val22

Do you and a comma on the end of the line? If so ... remove 2nd gsub call:

awk '{gsub(/ *[\r\n]+|, /, ",");$0=RS $0} NR>1' RS='str1' infile
1 Like

I am really, really sorry. It turns out that the lines of the file are ending with ^M, I saw it when I opened with vi. I removed it and it works. What a stupid mistake....and it is not the first time :frowning:
Guys, thank you very much for your kind support!!!