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
Share:
152,250
Danf
Author by

Danf

Updated on July 08, 2022

Comments

  • Danf
    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
    Kent about 11 years
    your both commands would generate ending comma
  • n3rV3
    n3rV3 about 11 years
    yes, i can fix it in awk but not in tr, give me a min.
  • sid_com
    sid_com about 11 years
    Even more cryptic: perl '-peeof||s|$/$|,|' file
  • Chris Seymour
    Chris Seymour about 11 years
    You only need to use -v if you want the variables available in the BEGIN block, slightly more concise awk '$1=$1' RS= OFS=, file.
  • Chris Seymour
    Chris Seymour about 11 years
    awk '{printf NR==1?$0:","$0}' file
  • n3rV3
    n3rV3 about 11 years
    cool, thanks for that
  • Ed Morton
    Ed Morton about 11 years
    Never use printf $0 as it will fail cryptically if $0 contains any printf formatting characters. The synopsis for printf is printf format, data so use printf "%s", $0 instead.
  • Ed Morton
    Ed Morton about 11 years
    For 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
    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
    n3rV3 about 11 years
    i still have a lot to learn. thanks
  • protist
    protist almost 11 years
    sid_com, I like your choice of substitution delimiters :D... perl -pe 'eof or s#\n$#,#' thing
  • Serhii Kuzmychov
    Serhii Kuzmychov over 10 years
    xargs 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
    Serhii Kuzmychov over 10 years
    usually xargs is not intended for t but it works :)
  • Serhii Kuzmychov
    Serhii Kuzmychov over 10 years
    and it more prefer way for every day use insted of the one bellow
  • Ray Hunter
    Ray Hunter about 10 years
    paste is an awesome command. Works like a champ!
  • Peter K
    Peter K over 9 years
    ok, 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
    NeoMorfeo over 8 years
    But how to accomplish that for a variable or a pipe? Update myself: Using -s without value .... | paste -d, -s
  • CoderGuy123
    CoderGuy123 almost 8 years
    While 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
    cadrian over 7 years
    except if there are spaces in your lines...
  • Serhii Kuzmychov
    Serhii Kuzmychov over 7 years
    sed -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
    Bee Kay over 7 years
    Thanks 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
    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
    Lazarus Thurston over 6 years
    too complicated. Please explain @protist.
  • jimh
    jimh over 6 years
    I 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.