frame multiple lines into one

Hi,

i have a file with contents like below ( any number of entries can be there)

111
222
333
444
555

i need to make another file with single line like below:

111,222,333,444,555 (without ending , )

TIA
Prvn

Try it with awk, use printf to precede every record except the first one with a comma.

Regards

Thank you franklin for your response.

As i dont know much about awk, could you please frame the command for me?

the output should be the file with single line as below (exactly):

'111','222','333','444','555' (with single quotes and without ending comma)

Thanks much
Prvn

Try this:

awk -v x="'" '{ s=s sprintf(x "%s" x ",", $0) } END { sub(",$", "", s); print(s) }' input_file.txt

Thanks Robotronic,

It worked great.

Prvn

A sed one

$ cat myfile | tr '\n' ','  | sed -e "s/[0-9]*/'&'/g" -e "s/,''//g"
'111','222','333','444','555'

//Jadu

And a paste-one :slight_smile:

paste -sd, filename

Another funny one:

awk 'END{print RS}$0=NR>1?x _$0_:_$0_' x=, _=\' ORS= filename

Hi,

Paste should be also ok for you, compare with the efficience of all the reply. If possible, share with us which one is the best one.

Thanks in advanced!

num=`cat file | wc -l`
while [ $num -ge 1 ]
do
str=`echo ${str}" - "`
num=`expr $num - 1`
done
cat file | paste $str