You are not logged in.

#1 2012-06-23 19:37:03

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Extract only numbers from perf stat data?

I am running a perf stat commands on some task and I would like to extract only numbers and separate them by a coma.

The origianl output of command like "perf stat sleep 1" is:

           0.435097  task-clock-msecs         #      0.000 CPUs 
                  1  context-switches         #      0.002 M/sec
                  0  CPU-migrations           #      0.000 M/sec
                160  page-faults              #      0.368 M/sec
            1250151  cycles                   #   2873.270 M/sec
            1030638  instructions             #      0.824 IPC  
             217124  branches                 #    499.024 M/sec
              15081  branch-misses            #      6.946 %    
              12414  cache-references         #     28.532 M/sec
               1908  cache-misses             #      4.385 M/sec

        1.001072685  seconds time elapsed

I would like to get it to output to something like:
0,681314, 0,001, 1, 0,001, 0..............1,001041631
Basically.. just numbers listed in sequential order.

I tried to use grep, but my skills are not that great in it yet... What can I do?

Last edited by kdar (2012-06-24 02:33:31)

Offline

#2 2012-06-23 19:43:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Extract only numbers from perf stat data?

awk.

something like

perf stat sleep 1 | awk '/^[ \t]*[0-9]/ { printf "%s,", $1; }'

should work.

Edit: oops, you want ALL numbers?  This gets the numbers in the first column.  Just the same awk would be the way to go.

edit: you could always do it in C:

#include <stdio.h>

int main() {
	int c, last_was_digit=0;
	while ( (c=getchar()) != EOF ) {
		if ( (c>47) && (c<58) ) { putchar(c); last_was_digit = 1; }
		else if (last_was_digit == 1) { putchar(','); last_was_digit = 0; }
	}
	return 0;
}

save the above as numbers.c, compile with `gcc -o numbers numbers.c`, then run with

perf stat sleep 1 | ./numbers

Last edited by Trilby (2012-06-23 20:10:29)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-06-23 19:56:17

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Re: Extract only numbers from perf stat data?

well. actually.. numbers in first column would be all I need. Everything in second colum is calculated form 1st.

Offline

#4 2012-06-23 20:09:18

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Extract only numbers from perf stat data?

The very simple awk line above would miss the 507 and the 334 in the cycles line though.

The c code should work.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2012-06-24 05:10:12

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Re: Extract only numbers from perf stat data?

I changed it a bit today..
Now, instead of perf stat sleep 1, I call perf stat -x' ' sleep 1, which outputs:

3.723143 task-clock
1 context-switches
0 CPU-migrations
147 page-faults
3710531 cycles
0 stalled-cycles-frontend
0 stalled-cycles-backend
1770507 instructions
196934 branches
69046 branch-misses

and to get just numbers... sequentially.. I call this:

(perf stat -x' ' sleep 1) 2>&1 | cut -d' ' -f1 | awk '/^[0-9]/ {printf "%s,", $l;}'

and I get this output:

4.089355,1,0,147,4059215,0,0,1808715,200557,73247,

Do you think I could improve that line a bit? (like if I could do the cutting with awk instead of having to use cut or other things...

Last edited by kdar (2012-06-24 05:12:06)

Offline

#6 2012-06-24 06:27:55

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: Extract only numbers from perf stat data?

you can remove the cut and use $1 instead of $l(that's one instead of el)

 [ ~ ]$ printf "1234 foob\n2345 bah\nfoobah 3456\n"
1234 foob
2345 bah
foobah 3456

 [ ~ ]$ printf "1234 foob\n2345 bah\nfoobah 3456\n" | awk '/^[0-9]/ {printf "%s,", $1}'
1234,2345,

HTH

Last edited by moetunes (2012-06-24 06:32:11)


You're just jealous because the voices only talk to me.

Offline

#7 2012-06-24 18:27:08

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Re: Extract only numbers from perf stat data?

ah that was 1..... I was thinking why l..

For some reason.. if I call perf stat -x' ' sleep 1 | awk '/^[0-9]/ { printf "%s,", $1; }'
this returns:

3.936767 task-clock
1 context-switches
0 CPU-migrations
146 page-faults
3945627 cycles
0 stalled-cycles-frontend
0 stalled-cycles-backend
1773871 instructions
196592 branches
72450 branch-misses

But if I call (perf stat -x' ' sleep 1) 2>&1 | awk '/^[0-9]/ { printf "%s,", $1; }'
This returns:

3.967286,1,0,147,3937673,0,0,1781241,197319,72790,

How does 2>&1 changes the results here?

Last edited by kdar (2012-06-24 18:49:59)

Offline

#8 2012-06-24 21:36:09

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: Extract only numbers from perf stat data?

Seems that "perf stat -x' ' sleep 1" writes to stderr(which is file descriptor 2) so you awk on nothing, since awk works on stdout. Redirecting stderr to stdout with 2>&1 you have something to awk on.


You're just jealous because the voices only talk to me.

Offline

#9 2012-06-25 03:34:40

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Re: Extract only numbers from perf stat data?

ah I see. Thanks.

I have another question... Is it possible with awk to make two redirections? for example... $1 to one file and $3 to another?

Nevermind.. I found how to do it. awk is sick smile

Last edited by kdar (2012-06-25 04:37:57)

Offline

#10 2012-06-25 04:36:53

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: Extract only numbers from perf stat data?

There's lots of awk stuff on the web, it just takes some reading and practise.

 [ ~ ]$ printf "one two three" | awk -v onefile="./one.txt" -v threefile="three.txt" '{print $1 > onefile; print $3 > threefile}'

 [ ~ ]$ cat ./one.txt
one

 [ ~ ]$ cat ./three.txt
three

HTH


You're just jealous because the voices only talk to me.

Offline

#11 2012-06-25 04:39:02

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Re: Extract only numbers from perf stat data?

moetunes wrote:

There's lots of awk stuff on the web, it just takes some reading and practise.

 [ ~ ]$ printf "one two three" | awk -v onefile="./one.txt" -v threefile="three.txt" '{print $1 > onefile; print $3 > threefile}'

 [ ~ ]$ cat ./one.txt
one

 [ ~ ]$ cat ./three.txt
three

HTH

thanks.

Offline

Board footer

Powered by FluxBB