Replace newline with comma and append quotes

Hi,

I have below requirement.

Apple
Orange
Banana

Required O/p in bash

'Apple,Orange,Banana'

Can you please help.

Hello Rtk,

Could you please try following.

awk 'BEGIN{s1="'";OFS=","} {val=val?val OFS $0:$0} END{print s1 val s1}'  Input_file

Thanks,
R. Singh

Does "'" within ' ' work correctly?
I think it must be "'\''" .
The following defines it outside the ' '

awk '{val=(val sep $0); sep=","} END {print (q val q)}' q="'"  Input_file

BTW val=val?val OFS $0:$0 might behave wrongly if the first lines are empty or zero.

Hi.

Also:

head z2 ; paste -d',' -s < z2

producing:

Apple
Orange
Banana
Apple,Orange,Banana

And, if single quotes are needed, pipe into:

sed -e "s/^/'/" -e "s/$/'/"

Best wishes ... cheers, drl