Make multiple lines into single quoted comma separated Linux

Hi,

I want to change a file file1.txt:

1234
3456
2345
6789
3456
2333
4444

As, file2.txt in Linux:

'1234','3456','2345','6789','3456','2333','4444'

Could someone please help me. (Single liner sed, awk will be welcome!)

You can put the following sed script into one line, but i wouldn't do so to have it remain easy to read:

sed -n ":start
        $    {
               s/^/'/
               s/$/'/
               H
               g
               s/\n//gp
               q
             }
             s/^/'/
             s/$/',/
             H
             n
             b start" /path/to/file1 > /path/to/file2

I hope this helps.

bakunin

Thanks for the reply.
This was my first post on this site, and I appreciate this quick response!

awk -vq="'" '{print (q $0 q)}' ORS=',' file1.txt > file2.txt

@SriniShoo: That leaves a trailing comma and no newline at the end..

Thanks Scrutinizer for finding the issue with the code.
Here is the solution

awk -vq="'" 'NR == 1{print (q $0 q); next} {print ("," q $0 q)} END {print "\n"}' ORS='' file

Some more:

awk '{printf q (NR>1?OFS:x) "%s", $1} END {print q}' q=\' OFS=",'" file
awk '{s=s (s?OFS:x) $1} END {print q s q}' q=\' OFS="','" file
awk '{$0=q $0 q; gsub(ORS,OFS)}1' q=\' RS= OFS="','" file

A simple pipeline which does not store the entire file in memory:

sed "s/.*/'&'/" file | paste -sd, -

A simpler alternative to bakunin's sed-only contribution:

sed -n "s/.*/'&'/; H; \${g; s/\n//; s/\n/,/g; p;}" file

Regards,
Alister