Comma padded.. Output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comma padded.. Output
# 1  
Old 11-11-2014
Comma padded.. Output

Hello, here is the outout of the command below.. Can someone please tell me how to get the output as below

output needed:
Code:
18914,30716,17051,4139,14155...

( no comma for the last value)
Code:
ps -e -o pcpu,pid,user,tty,args | sort -n -k 1 -r | head | awk '{print $2}'
18914
30716
17051
4139
14155
18618
4196
3883
26680
26099


Last edited by Scott; 11-11-2014 at 06:37 AM.. Reason: Added code tags
# 2  
Old 11-11-2014
Hello Kamathg,

Kindly use code tags as per forum rules while posting codes and commands. You can try with ORS="," af last of your awk code, it is just a hint you can try reading man awk for same and try to do some search by your self. Kindly let us know if you face any issues after seraching and trying from yourself.


Thanks,
R. Singh
# 3  
Old 11-11-2014
Quote:
. . . ( no comma for the last value) . . .
and
Quote:
ORS=","
are not compatible. sort -k1 is redundant. Try
Code:
ps -e -o pcpu,pid,user,tty,args | sort -nr | awk 'NR>10 {printf "\n"; exit} {printf "%s%s", DL, $2; DL=","}' 
3972,31820,4053,1867,31795,31790,31775,3950,3976,2439

# 4  
Old 11-11-2014
Many thanks indeed.. Appreciate it
# 5  
Old 11-11-2014
With e.g. procps-ng version 3.3.9, you can even specify the sort field and order using the k specifier:
Code:
ps -e -o pcpu,pid,user,tty,args k-pcpu --no-headers |  awk 'NR>10 {printf "\n"; exit} {printf "%s%s", DL, $2; DL=","}' 
3972,31820,4053,1867,31795,31775,31790,2439,3950,3976

# 6  
Old 11-11-2014
Solution for your own approach, simply extend your existing combination of commands with
Code:
| paste -sd,

This User Gave Thanks to junior-helper For This Post:
# 7  
Old 11-11-2014
Indeed the ps header can appear in the output, for example
Code:
# ps -e -o pcpu,pid,user,tty,args | sort -n -k 1 -r | awk 'NR>10 {exit} {print $2}' | paste -sd,
3339,20306,3340,1746,PID,32756,32755,32753,32607,32605

The Posix-compliant solution is to give all ps columns null names:
Code:
ps -e -o pcpu= -o pid= -o user= -o tty= -o args= | ...

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

Printf padded string

Is possible to print padded string in printf? Example echo 1 | awk '{printf("%03d\n", $1)}' 001I want S1 S11 S2 S21to be padded as: S01 S11 S02 S21Thanks! (26 Replies)
Discussion started by: yifangt
26 Replies

3. Shell Programming and Scripting

Need comma separated output

Hi, I am having the file with server names & its corresponding process, i need your help how to convert into comma separated output between server & app #cat apps.txt Server1 oracle was Server2 http webadmin Server3 tsm db2 My requirement is like below. Server1,oracle/was... (5 Replies)
Discussion started by: ksgnathan
5 Replies

4. Shell Programming and Scripting

Linux - Script to generate the output delimited by Comma/Pipe

Hi All, I have a requirement where I need to go to a directory, list all the files that start with person* (for eg) & read the most recent file from the list of files. While browsing through the forum, i found that the command ls -t will list the files. I am trying to generate the output... (1 Reply)
Discussion started by: dsfreddie
1 Replies

5. Shell Programming and Scripting

Output of command in comma separated list

Hi; I have an output of a particular command say $command fstl:r-x ajay:r-x how can i get this in comma separated list, eg: fstl:r-x,ajay:r-x Thnks; (4 Replies)
Discussion started by: ajaypadvi
4 Replies

6. Shell Programming and Scripting

Perl script to parse output and print it comma separated

I need to arrange output of SQL query into a comma separated format and I'm struggling with processing the output... The output is something like this: <Attribute1 name><x amount of white spaces><Atribute value> <Attribute2 name><x amount of white spaces><Atribute value> <Attribute3... (2 Replies)
Discussion started by: Juha
2 Replies

7. Shell Programming and Scripting

Remote command output to file comma delimited

All, I am trying to run a command that will get the serial number, model, and hostname of a server and then output the 3 fields comma delimited. Then a new line to separate the 3 fields for each host. So, I run a for loop that reads in the list of hosts in a file and then runs these sudo... (0 Replies)
Discussion started by: markdjones82
0 Replies

8. Shell Programming and Scripting

perl help for comma seperated output

Hi, how can i make a comma seperated output summary. i attached the sample log file. I have to capture these data in the log file. Arcotid Time Stamp, Username, Success, Failure, Error Code, Error Message In the log snippet the userID can be found in- Code Arcot Native Server:... (3 Replies)
Discussion started by: namishtiwari
3 Replies

9. Shell Programming and Scripting

Comma Delimited Output w/ egrep

Hi all, I have an input file that I am pulling out certain phases using the following commands: cat /nodes.txt | egrep -e 'OSVersion|PrimaryNodeName' Currently the output looks like this: OSVersion - 5.0 PrimaryNodeName - serverA OSVersion - 5.0 PrimaryNodeName - serverB OSVersion... (2 Replies)
Discussion started by: indianadoug
2 Replies
Login or Register to Ask a Question