![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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. |
|
||||
|
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
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 | ||
|
|
![]() |
| Bookmarks |
| Tags |
| comma separated, transpose |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|