I have more than 1000 lines in a file. The file starts as follows (line numbers added):
Station Name
Station Code
A N DEV NAGAR
ACND
ABHAIPUR
AHA
ABOHAR
ABS
ABU ROAD
ABR
I need to convert this to a file, with comma separated entries by joining every two lines. The final data should look like
Station Name,Station Code
A N DEV NAGAR,ACND
ABHAIPUR,AHA
ABOHAR,ABS
ABU ROAD,ABR
...
What I was trying was – trying to write a shell script and then echo
them with comma in between. But I guess a simpler effective one-liner would do the job here may be in sed
/awk
.
Any ideas?
Best Answer
Simply use
cat
(if you like cats ;-)) andpaste
: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):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, whichpaste
happily glues to the first one. The-d
option sets the delimiter to be a comma rather than a tab.Alternatively, do
P.S. Yes, one can simplify the above to
or
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: