1

I am outputting the results of a timed executable to a file that has run 10 times. I have all the times in a file, and I need to find the best, worst, and average user times from this file. I was thinking about using a combination of awk to print the first column, but I am unsure how to strip "user" from the line so I can sort it numerically.

Also, I would need to get rid of the even rows, since they have information about the inputs and outputs and things that don't matter to these calculations. A sample of the time file is below:

1.12user 0.00system 0:01.12elapsed 99%CPU (0avgtext+0avgdata 5408maxresident)k
0inputs+0outputs (0major+398minor)pagefaults 0swaps

Does anyone have any idea of how I would do this?

Any help would be appreciated! Thank you.

1
  • you can get best,worst,avg in one-shot. updated answer. Commented Feb 16, 2013 at 23:58

3 Answers 3

1

is this ok for you?

awk -F'user' 'NR%2{print $1}' file 

with an example:

kent$  cat file
1.11user 0.00system 0:01.12elapsed 99%CPU (0avgtext+0avgdata 5408maxresident)k
0inputs+0outputs (0major+398minor)pagefaults 0swaps
1.10user 1.00system 0:01.12elapsed 99%CPU (0avgtext+0avgdata 5408maxresident)k
0inputs+0outputs (0major+398minor)pagefaults 0swaps
1.12user 2.00system 0:01.12elapsed 99%CPU (0avgtext+0avgdata 5408maxresident)k
0inputs+0outputs (0major+398minor)pagefaults 0swaps
1.13user 3.00system 0:01.12elapsed 99%CPU (0avgtext+0avgdata 5408maxresident)k
0inputs+0outputs (0major+398minor)pagefaults 0swaps

kent$  awk -F'user' 'NR%2{print $1}' file
1.11
1.10
1.12
1.13

Even this maybe?

 awk -F'user' 'NR%2{a[++i]=$1;s+=$1}END{asort(a);print "best: "a[1];print "worst: "a[length(a)];print "avg: "s/length(a)}' file   

the above example will output:

best: 1.10
worst: 1.13
avg: 1.115
Sign up to request clarification or add additional context in comments.

2 Comments

Yes! Thanks so much! I will vote up as soon as I have the reputation to do so.
Is there a way to print best and worst with head and tail after I sort the list, out of curiosity? I thought I got it to work, but each one prints 01 about 10 times. Odd
1
tr -s ' '  '[:alpha:]' < inputfile | awk '{print $1, $2, $3} ' > newfile

There other variations to do the same thing.

1 Comment

I tried that, and it returns: tr: when translating, the only character classes that may appear in string2 are upper' and lower' '
1

If you're only interested in the initial number #user.

cat txt | paste - - | cut -d' ' -f1  | tr -d "[:alpha:]" | sort -n

gives:

1.12
1.13
1.14
1.15
1.16

on my example of your txt with numbers changed to prove sorting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.