The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to transpose a table of data using awk ahjiefreak Shell Programming and Scripting 2 07-28-2008 11:43 PM
How to transpose data elements in awk ahjiefreak Shell Programming and Scripting 2 05-13-2008 04:44 AM
How do I transpose a column of results to a row m223464 Shell Programming and Scripting 6 05-06-2008 07:33 AM
Row to column transpose videsh77 Shell Programming and Scripting 10 06-16-2007 12:54 PM
transpose command su_in99 UNIX for Dummies Questions & Answers 3 05-16-2007 05:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2009
iamwha1am iamwha1am is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 4
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.
  #2 (permalink)  
Old 03-17-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,310

Code:
sessList=$(ps -ef | awk -v sq="'" 'NR > 1 { printf sq "%s" sq ",", $2 }' )
  #3 (permalink)  
Old 03-18-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,047
Code:
sed -e "s/^/'/" -e "s/$/'/" a.txt | nawk '{str=sprintf("%s,%s",str,$0)}
END{
	sub(/,/,"",str)
print str
}'
  #4 (permalink)  
Old 03-18-2009
iamwha1am iamwha1am is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 4
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.
  #5 (permalink)  
Old 03-18-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
if you have python
Code:
# 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'
Sponsored Links
Closed Thread

Bookmarks

Tags
comma separated, transpose

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0