Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Join lines into one line

SOLVED
Go to Solution
Frequent Advisor

Join lines into one line

Hi,

I have a text file which includes undefined number of lines as follows:
====file.txt====
line1
line2
line3
line4
[...]
lineN
============

What command should I use to have the output like that:
line1,line2,line3,line4,[...],lineN

awk is very welcome. Thanks in advance


12 REPLIES
Honored Contributor [Founder]

Re: Join lines into one line

tr '\n' ',' < file.txt

MK
MK
Honored Contributor [Founder]

Re: Join lines into one line

> tr '\n' ',' < file.txt

And, to trim off the final comma:

< file.txt tr '\n' ',' | sed -e 's/,$//'
Honored Contributor [Founder]

Re: Join lines into one line

or

# cat file.txt | xargs
****
Unix operates with beer.
Frequent Advisor

Re: Join lines into one line

xargs uses space ' ' instead of comma.
Honored Contributor [Founder]

Re: Join lines into one line

.. if you want it comma-separated:

# cat file.txt | xargs | tr " " ","
****
Unix operates with beer.
Honored Contributor [Founder]

Re: Join lines into one line

> awk is very welcome. Thanks in advance

here is your AWKward solution:

# awk '{printf $0","} END {print}' file.txt
****
Unix operates with beer.
Honored Contributor [Founder]

Re: Join lines into one line

> # cat file.txt | xargs | tr " " ","

Not so good if the lines contain spaces:

dyi # cat file.txt
line 1
line 2
line 3

dyi # cat file.txt | xargs | tr " " ","
line,1,line,2,line,3

But (this time on a real HP-UX system):

dyi # ( tr '\n' ',' ; echo '' ) < file.txt | sed -e 's/,$//'
line 1,line 2,line 3


(Some "sed" programs don't do well with
unterminated lines, I see. Everything's
complicated.)
Honored Contributor [Founder]

Re: Join lines into one line

> # awk '{printf $0","} END {print}' file.txt

Around here, that still has the extra comma
at the end:

dyi # awk '{printf $0","} END {print}' file.txt
line 1,line 2,line 3,

(But it does better with embedded spaces.)
Honored Contributor [Founder]

Re: Join lines into one line

> Around here, that still has the extra comma
at the end

nope, on my linux box it duplicates the last line and so I didn't end up with an extra comma. I just didn't recognize the duplicate.

basically I wanted this version, but somehow the reference to the last line didn't work:

# awk 'NR != "$NR" {printf $0","} END {print}' test

so I ended up with this:

# awk '!(NR-1) {printf $0} NR-1 {printf ","$0} END {print ""}'

which works on linux. (too sad I don't have an HP-UX access at the moment :(
****
Unix operates with beer.
Honored Contributor [Founder]

Re: Join lines into one line

I know this horse has been beaten but here is how I would deal with the 'extra comma' problem in awk:

$ awk '{printf comma $0; comma=","}' x.txt

so we print a comma BEFORE each fresh line, but we don't want one on the first line, so we set a symbol to the value comma, after the first print.
(and again and again, but awk don't mind that :-).

fwiw,
Hein

Honored Contributor [Founder]

Re: Join lines into one line

Hi,

#AUX=`cat file.txt|tr "\n" ","`
#echo $AUX > file.aux
#cat file.aux
line1,line2,line3,line4,...,

Rgds.
Highlighted
Honored Contributor [Founder]

Re: Join lines into one line

Hein,

>so we print a comma BEFORE each fresh line, but we don't want one on the first line

that's exactly what I did with

# awk '!(NR-1) {printf $0} NR-1 {printf ","$0} END {print ""}'

but you solution is genious! Maybe an END section just to echo a linefeed after the last line... ;)
****
Unix operates with beer.