Remove characters

How can i parse “%CPU COMMAND”-header away and only include cpu-usage and process name in list?

set the_list to do shell script "ps -Acr -o %cpu,command"

From this:
“%CPU COMMAND
12.1 Safari
7.2 Finder”

to this:

{“12.1”, “Safari”, “7.2”, “Finder”}

Hi cirno,

something like this, but there is certainly a more sophisticated solution :wink:

set p to paragraphs of (do shell script "ps -Acr -o %cpu,command")
set the_list to {}
repeat with i from 3 to count p
	copy word 1 of item i of p to end of the_list
	copy text from words 2 to -1 of item i of p as Unicode text to end of the_list
end repeat

Try something like this:

do shell script "/bin/ps -Acr -o %cpu,command | /usr/bin/ruby -ne 'STDIN.readlines.each {|thisLine| puts thisLine.strip.sub(\" \", \"\\n\")}'"
set psList to paragraphs of result

easiest yet:

set the_list to do shell script "ps -Acr -o %cpu,command | sed 1d"

That doesn’t put anything into a list, waltr.

That’s interesting, Bruce. In 10.4 it’s a full list of processes and cpu usage (at least on MM). Like so–

EDIT: Now, reading later posts, I see - Script Debugger can fool you - I didn’t see that the list variable wasn’t populated. :rolleyes:

I believe that’s the expected behavior; I assumed that cirno was giving an example.

hi bruce,

my bad:

set the_list to do shell script "ps -Acr -o %cpu,command | sed 1d"

set psList to paragraphs of result

:lol:

EDIT: actually, the above is wrong. this gets rid of leading spaces in each item:

set the_list to do shell script "ps -Acr -o %cpu,command | sed 1d | sed 's/^ *//'"

set psList to paragraphs of result

still i find that easier–to each his/her own i suppose.

That still doesn’t separate the percentage from the name.

hi bruce,

which was not part of the request of the OP.

this is too vague to know if the percentage should be separated.

you stalking me bruce?:lol:

EDIT: on rereading, i did not notice this:

easy enough. change one word:

set the_list to do shell script "ps -Acr -o %cpu,command | sed 1d | sed 's/^ *//'"

set psList to words of result

but i still think you are crossing a line bruce. why not suggest this one word change? surely you could have. PM me directly if you have a personal problem with me. i’m sure we can work it out.

SECOND EDIT: i’m seeing a further problem with this. perhaps you are not stalking. i’ll ponder and repost. apologies if i’m out of line here.

cheers.

Something like this, perhaps:


set Stats to {}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
repeat with I in paragraphs of (do shell script "ps -Acr -o %cpu,command | sed 1d | sed 's/^ *//'")
	tell (text items of I) to set end of Stats to {it's rest as text, it's beginning as number}
end repeat
set AppleScript's text item delimiters to tid
Stats

hm, my (already forgotten) version is a bit faster and
creates the list items as requested :wink:

True, Stefan. It’s easy to do - :lol: - get swept up in the flow and forget we’ve been there before.

On my machine, yours takes (timed with GetMilliSec) 20.5 seconds for 500 repeats, and mine take 44.67, same conditions.
You win, 2.2 to 1 :slight_smile:

just for sports and fun, here is the one-liner:

set p to paragraphs 3 thru -1 of (do shell script "ps -Acr -o %cpu,command | awk '{print $1} {for (i=2; i<=NF; i++) printf(\"%s \", $i); printf \"\\r\";}'")

ok, i went through this & i’m willing to admit i’m wrong. this:

set the_list to do shell script "ps -Acr -o %cpu,command | awk  '/[0-9]/ {OFS = \"" & "\\" & "r" & "\";  if ($4 != \"\") {print $1, $2 \" \" $3 \" \" $4} else if ($3 != \"\") {print $1, $2 \" \" $3} else {print $1, $2}}'"

set psList to paragraphs of result

somewhat does what’s requested, but only if the process has 3 or less words in it. it’s definitely inferior to the other scripts. i may bang my head against this a bit further, but i’ll have to do it at another time.

cheers.

after looking at stephan’s ‘awk’ command, i combined his and mine:

set psList to paragraphs of (do shell script "ps -Acr -o %cpu,command | awk  '/[0-9]/ {{print $1}; {for (i=2; i<=NF; i++) if (i > 2) {printf(\" \" \"%s\", $i)} else {printf(\"%s\", $i)}printf \"\\r\"}}'")

it tightens up the spaces a bit and makes a really clean list. not a terribly easy thing to come up with.

cheers.

Well, I was on a break while traveling, and wasn’t able to test anything. (Yes, I checked this on the road. I’m. dedicated. ;)) I just thought you may not have caught the “separation” part. Plus, I can’t recall having ever used words in a script; I probably wouldn’t have thought of it if I did have the chance.

Um, only if you want me to. :expressionless: Don’t worry, if I had a problem, you would know it (privately).

About Adam’s list; This would work:

	tell (text items of I)
		set end of Stats to it's beginning as number
		set end of Stats to it's rest as text
	end tell

Thanks for help, i have learned alot.

Only problem with these are that widgets are listed as “DashboardClient”.

…"System Events ", “0.0”, "DashboardClient ", “0.0”, "DashboardClient ", "TextEdit "…

Here is few ways to list Widget names, but how i combine your code and this Widget names code.
http://bbs.applescript.net/viewtopic.php?id=17354

These lists of lists are still too complicated for me. I tried to remove all entries which has 0.0 so i added:
set my_result to every item in Stats whose second item is not “0.0” but it dont work?


set Stats to {}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
repeat with I in paragraphs of (do shell script "ps -Acr -o %cpu,command | sed 1d | sed 's/^ *//'")
	tell (text items of I) to set end of Stats to {it's rest as text, it's beginning as number}
end repeat
set AppleScript's text item delimiters to tid
Stats

Hi cirno,

try this:

set Stats to {}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
repeat with I in paragraphs of (do shell script "ps -Acr -o %cpu,command | sed 1d | sed 's/^ *//'")
	tell (text items of I) to if text item 1 is not "0.0" then set end of Stats to {it's rest as text, it's beginning as number}
end repeat
set AppleScript's text item delimiters to tid
Stats

PS: I get a “can’t make … into number” error with “…beginning as number”, “…beginning as real” works