Transpose Rows

Hi,

Am trying to transpose a set of rows into a set of comma separated values.

For eg. if the output of

ps -ef | tail +2 | awk 'BEGIN{ FS=" " } { print $2 }'

is

0
1
3
4

I need to transpose it to -

'0','1','3','4'

Am currently trying -

ps -ef | tail +2 | awk 'BEGIN{ FS=" " } { print $2 }' | tr '\n' ',' > tmpFile
sessList=`cat tmpFile | sed s/.$/\'/ | sed s/^/\'/ | sed s/\,/\'\,\'/ `
echo $sessList

However that does not seem to work.

Any pointers towards achieving this more efficiently/elegantly will be of great help.

sessList=$(ps -ef | awk -v sq="'" 'NR > 1 { printf sq "%s" sq ",", $2 }' )
sed -e "s/^/'/" -e "s/$/'/" a.txt | nawk '{str=sprintf("%s,%s",str,$0)}
END{
	sub(/,/,"",str)
print str
}' 

Thanks Johnson and Cherry for the responses.

The output from

sessList=$(ps -ef | awk -v sq="'" 'NR > 1 { printf sq "%s" sq ",", $2 }' )

looks like -
'0','1','3','4',

There is an additional comma in the end.

The Output from Cherry's command is exactly the one I was looking for.

However, can you please help me understand the syntax (am still grappling with sed and awk using google) so that I can change it if required in the future.

if you have python

# ps -eo pid | python -c "import sys;print '\'' +  '\',\''.join(sys.stdin.read().split()[1:]) + '\''"
'1','2','3','4','5','6','7','8','9','12','13','81','82','83','84','85','296','297','298','337','667','668','680','683','795','889','1203','1417','1525','1649','1792','1793','2008','2010','2012','2014','2016','2018','2512','2527','2542','2639','2790','2793','2831','3277','3324','3360','3488','3520','3595','3675','3678','3731','3820','3828','3845','3882','3890','3897','3990','4119','4137','4162','4174','4182','4190','4200','4201','4303','4356','4488','4489','4490','4491','4492','4493','4529','4530','4531','4532','4533','4568','4768','5016','5017','5040','5078','5082','5083','5115','5118','5120','5122','5127','5129','5130','5132','5134','5135','5140','5142','5146','5150','5152','5163','5166','5189','5356','5357','5477','5478','5482','5483','5494','5759','5784','5801','5817','5835','5868','5887','5903','5930','5963','5965','7577','11900','3766','17617','17621','17639','17662','17675','17769','17783','20002','22678','23660','23722','23723'