使用bash脚本从字母数字文本文件中检索数字

时间:2021-08-10 00:09:15

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.

我将定时可执行文件的结果输出到已运行10次的文件。我一直在一个文件中,我需要从这个文件中找到最佳,最差和平均的用户时间。我正在考虑使用awk的组合打印第一列,但我不确定如何从行中删除“用户”,以便我可以用数字排序。

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.

任何帮助,将不胜感激!谢谢。

3 个解决方案

#1


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

#2


0  

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

There other variations to do the same thing.

还有其他变化做同样的事情。

#3


0  

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

如果您只对初始号码#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.

在我的txt示例中,数字已更改以证明排序。

#1


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

#2


0  

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

There other variations to do the same thing.

还有其他变化做同样的事情。

#3


0  

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

如果您只对初始号码#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.

在我的txt示例中,数字已更改以证明排序。