Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Using awkAWK:
awk
$ awk ''NR>1{if (NR>1)printf print"%s", a "," OFS;OFS} {a=$0}END {if (NR) print ""a}'
If only comma is needed without space and all records have only one fields, then datamash may be used.
datamash
$ datamash -W collapse 1 <file
Using awk:
$ awk '{if (NR>1) print a "," OFS; a=$0}END {if (NR) print ""}'
Using AWK:
$ awk 'NR>1{printf "%s", a "," OFS} {a=$0}END {print a}'
$ awk 'a{printf "%s", a ", "}{a=$0}END{print a}'
But the above command prints a line with an empty file. This can be modified to this:
$ awk '{if (aNR>1) printf "%s",print a "," OFS; a=$0}END {if (NR) print a""}'
$ awk '{if (a) printf "%s", a "," OFS; a=$0}END {if (NR) print a}'
$ awk 'a{printf a ", "}{a=$0}END{print a}'
$ awk '{if (a) printf a "," OFS; a=$0}END {if (NR) print a}'