Re: Join lines into one line (480 Views)
Reply
Frequent Advisor
wojtek75
Posts: 49
Registered: ‎07-10-2007
Message 1 of 13 (480 Views)
Accepted Solution

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


Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 13 (480 Views)

Re: Join lines into one line

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

MK
MK
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 3 of 13 (480 Views)

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
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 4 of 13 (480 Views)

Re: Join lines into one line

or

# cat file.txt | xargs
****
Unix operates with beer.
Frequent Advisor
wojtek75
Posts: 49
Registered: ‎07-10-2007
Message 5 of 13 (480 Views)

Re: Join lines into one line

xargs uses space ' ' instead of comma.
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 6 of 13 (480 Views)

Re: Join lines into one line

.. if you want it comma-separated:

# cat file.txt | xargs | tr " " ","
****
Unix operates with beer.
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 7 of 13 (480 Views)

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
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 8 of 13 (480 Views)

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
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 9 of 13 (480 Views)

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
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 10 of 13 (480 Views)

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
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 11 of 13 (480 Views)

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
Jose Mosquera
Posts: 1,013
Registered: ‎06-27-2002
Message 12 of 13 (480 Views)

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.
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 13 of 13 (480 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.