Column to row

LIN7-1274
LIN7-1176
LIN5-1271
LIN6-1186

to

LIN7-1274,LIN7-1176,LIN5-1271,LIN6-1186

Hello yanglei_fage,

Following may help in same.

awk '1; END{ORS=""; print "\b \n"}' ORS="," Input_file

Output will be as follows.

LIN7-1274,LIN7-1176,LIN5-1271,LIN6-1186

Thanks,
R. Singh

Also try

xargs < infile | tr ' ' ,
pr -ts, infile --columns $(wc -l < infile)

Adding a <backspace> char at the end will make the output to screen seem to be what is requested, but checking with od will show there are undesired characters:

awk '1; END{ORS=""; print "\b \n"}' ORS="," file4 | od -tx1c
0000000  4c  49  4e  37  2d  31  32  37  34  2c  4c  49  4e  37  2d  31
          L   I   N   7   -   1   2   7   4   ,   L   I   N   7   -   1
0000020  31  37  36  2c  4c  49  4e  35  2d  31  32  37  31  2c  4c  49
          1   7   6   ,   L   I   N   5   -   1   2   7   1   ,   L   I
0000040  4e  36  2d  31  31  38  36  2c  08  20  0a
          N   6   -   1   1   8   6   ,  \b      \n

Try this to avoid this trap:

sed -n '1h;1!H;${g;s#\n#,#g;p}' file | od -tx1c
0000000  4c  49  4e  37  2d  31  32  37  34  2c  4c  49  4e  37  2d  31
          L   I   N   7   -   1   2   7   4   ,   L   I   N   7   -   1
0000020  31  37  36  2c  4c  49  4e  35  2d  31  32  37  31  2c  4c  49
          1   7   6   ,   L   I   N   5   -   1   2   7   1   ,   L   I
0000040  4e  36  2d  31  31  38  36  0a
          N   6   -   1   1   8   6  \n

If your sed doesn't allow for the \n representation of <newline> try the [[:cntrl:]] class.

1 Like

No need to pipe the output to tr as xargs will strip the input file of the newlines simply with... xargs <file

Op should be csv, I think

1 Like