scoots
scoots

Reputation: 294

Retrieving numbers from an alphanumeric text file using a bash script

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.

Upvotes: 1

Views: 123

Answers (3)

sotapme
sotapme

Reputation: 4903

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.

Upvotes: 1

Kent
Kent

Reputation: 195099

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

Upvotes: 1

jim mcnamara
jim mcnamara

Reputation: 16389

tr -s ' '  '[:alpha:]' < inputfile | awk '{print $1, $2, $3} ' > newfile

There other variations to do the same thing.

Upvotes: 1

Related Questions