Turning multiple lines into one comma separated line
152,250
Solution 1
Using paste command:
paste -d, -s file
Solution 2
file
aaa
bbb
ccc
ddd
xargs
cat file | xargs
result
aaa bbb ccc ddd
xargs improoved
cat file | xargs | sed -e 's/ /,/g'
result
aaa,bbb,ccc,ddd
Solution 3
There are many ways it can be achieved. The tool you use mostly depends on your own preference or experience.
Using tr command:
tr '\n' ',' < somefile
Using awk:
awk -F'\n' '{if(NR == 1) {printf $0} else {printf ","$0}}' somefile
Solution 4
xargs -a your_file | sed 's/ /,/g'
This is a shorter way.
Solution 5
based on your input example, this awk line works. (without trailing comma)
awk -vRS="" -vOFS=',' '$1=$1' file
test:
kent$ echo "foo
bar
qux
zuu
sdf
sdfasdf"|awk -vRS="" -vOFS=',' '$1=$1'
foo,bar,qux,zuu,sdf,sdfasdf
Author by
Danf
Updated on July 08, 2022Comments
-
Danf almost 2 years
I have the following data in multiple lines:
foo bar qux zuu sdf sdfasdf
What I want to do is to convert them to one comma separated line:
foo,bar,qux,zuu,sdf,sdfasdf
What's the best unix one-liner to do that?
-
Kent about 11 yearsyour both commands would generate ending comma
-
n3rV3 about 11 yearsyes, i can fix it in awk but not in tr, give me a min.
-
sid_com about 11 yearsEven more cryptic:
perl '-peeof||s|$/$|,|' file
-
Chris Seymour about 11 yearsYou only need to use
-v
if you want the variables available in the BEGIN block, slightly more conciseawk '$1=$1' RS= OFS=, file
. -
Chris Seymour about 11 years
awk '{printf NR==1?$0:","$0}' file
-
n3rV3 about 11 yearscool, thanks for that
-
Ed Morton about 11 yearsNever use
printf $0
as it will fail cryptically if $0 contains any printf formatting characters. The synopsis for printf isprintf format, data
so useprintf "%s", $0
instead. -
Ed Morton about 11 yearsFor clarity and to avoid getting surprised when you later do try to use a variable in a BEGIN block, I'd always use -v unless setting a variable to different values between files. FYI omitting the space between -v and the variable name makes your script un-necessarily gawk-specific so I'd use
-v RS=
instead of-vRS=
. -
Kent about 11 years@EdMorton thx for the comment. (the "space" between -v and va). I learned many tips from you :). Personally I always use
-v
too. -
n3rV3 about 11 yearsi still have a lot to learn. thanks
-
protist almost 11 yearssid_com, I like your choice of substitution delimiters :D...
perl -pe 'eof or s#\n$#,#' thing
-
Serhii Kuzmychov over 10 yearsxargs turns multi-line into one line space-separated, then sed replaces all space by ','(or ' ,' if you will use sed -e 's/ /\ ,/g' ( by the way -e can be omited)
-
Serhii Kuzmychov over 10 yearsusually xargs is not intended for t but it works :)
-
Serhii Kuzmychov over 10 yearsand it more prefer way for every day use insted of the one bellow
-
Ray Hunter about 10 yearspaste is an awesome command. Works like a champ!
-
Peter K over 9 yearsok, even more cryptic perl: perl -l54 -pe 'eof and $\=""' file (and you can go with empty script body if you can tolerate last coma)
-
NeoMorfeo over 8 yearsBut how to accomplish that for a variable or a pipe? Update myself: Using -s without value .... | paste -d, -s
-
CoderGuy123 almost 8 yearsWhile this works for when one wants to convert newlines (
\n
) into commas, it seems that one cannot use this method if one needs to convert newlines into ",
" which is required for many other commands. -
cadrian over 7 yearsexcept if there are spaces in your lines...
-
Serhii Kuzmychov over 7 yearssed -e 's/ /+/g' test |xargs|sed 's/ /,/g;s/+/ /g' it was just an idea how to use xargs :) do not think it is seriously
-
Bee Kay over 7 yearsThanks for the step by step! I had a file with one entry per line, (fn, ln,id,status, blank line.) I needed it in CSV, so I swapped "blank line" with an unused symbol (sed -e 's/^$/#/') before using the mod you've explained.
-
Carson Anderson about 7 years@NeoMorfeo You can use a
-
to read from pipe as well. Ex:echo -e "1\n2\n3" | paste -d, -s -
-
Lazarus Thurston over 6 yearstoo complicated. Please explain @protist.
-
jimh over 6 yearsI found this to be the solution I was looking for, not the accepted one because it works for pipes and multiple files and you can pass it to tr -s " " "\t" if you want to add tabs and so forth. Thank you.