Text processing - join every two lines with commas
Simply use cat
(if you like cats ;-)) and paste
:
cat file.in | paste -d, - - > file.out
Explanation: paste
reads from a number of files and pastes together the corresponding lines (line 1 from first file with line 1 from second file etc):
paste file1 file2 ...
Instead of a file name, we can use -
(dash). paste
takes first line from file1 (which is stdin). Then, it wants to read the first line from file2 (which is also stdin). However, since the first line of stdin was already read and processed, what now waits on the input stream is the second line of stdin, which paste
happily glues to the first one. The -d
option sets the delimiter to be a comma rather than a tab.
Alternatively, do
cat file.in | sed "N;s/\n/,/" > file.out
P.S. Yes, one can simplify the above to
< file.in sed "N;s/\n/,/" > file.out
or
< file.in paste -d, - - > file.out
which has the advantage of not using cat
.
However, I did not use this idiom on purpose, for clarity reasons -- it is less verbose and I like cat
(CATS ARE NICE). So please do not edit.
Alternatively, if you prefer paste to cats (paste is the command to concatenate files horizontally, while cat concatenates them vertically), you may use:
paste file.in | paste -d, - -
In case anyone landing here is looking to combine all lines into a CSV one liner, try
cat file | tr '\n' ','
sed 'N;s/\n/,/' file
Using sed, join(N) every 2 lines, and replace the newline(\n) with ",".