问题描述
我的文件中包含以下内容:
I have file with content:
file.txt:
Iteration 1
RAM: +456ms
Cache: +142ms (total +417ms)
Iteration 2
Spec: +152ms
Cache: +149ms (total +413ms)
Iteration 3
RAM: +184ms
Spec: +172ms
Searchms: +131ms (total +385ms)
First launch 4
RAM: +149ms
Searchms: +188ms
在此文件中,每个First launch
之间的内容可以不同,它不是固定的(例如:第一次启动3包含三个元素,而第一次启动2的内容仅包含2个元素),因此,在First launch
模式,一开始是未知的.
In this file between every First launch
, content can be different, it is not fixed ( for example: First launch 3 contain three elements while First launch 2 contents only 2 elements), so any number of content can be between the First launch
pattern which is not known at the beginning.
预期输出:
RAM 456 184 149
Cache 142 149
Spec 152 172
Searchms 131 188
由于不知道确切的方法,所以我尝试了这段代码.
Due to not knowing exact approach,I have tried this code.
我的代码:
for i in {1..4}
do
awk "/First launch $i/{flag=1;next} /First launch $((i+1))/{flag=0} flag" file.txt> fl$i.txt
sed -i 's/\+//g' fl$i.txt
sed -i 's/://g' fl$i.txt
sed -i 's/(.*//g' fl$i.txt
sed -i 's/ms//g' fl$i.txt
awk '{print $1 "\t" $2}' fl$i.txt
done
我的输出有两个问题:我正在为每个模式生成文件,这是错误的.我也想在此时间之后删除ms
,但它也从模式名称中删除ms
(例如:Searchms to Search)
My output has two issues:I am generating the file for each pattern which is wrong. Also I wanted to remove ms
after the time but it also removes the ms
from the pattern name (ex: Searchms to Search)
输出:
fl1.txt:
RAM 456
Cache 142
fl2.txt :
Spec 152
Cache 149
fl3.txt :
RAM 184
Spec 152
Search 131
fl4.txt :
RAM 149
Search 188
请向我建议一种在不产生任何额外文件的情况下获得预期输出的方法,并且要在删除时间后删除ms
.
Please suggest me an approach to get the expected output without generating any extra file with the constraint of removing ms
after the time.
推荐答案
$ cat tst.awk
BEGIN { FS="[: ]+" }
/:/ { vals[$1] = vals[$1] OFS $2+0 }
END { for (key in vals) print key vals[key] }
$ awk -f tst.awk file
Cache 142 149
RAM 456 184 149
Searchms 131 188
Spec 152 172
这篇关于以bash的特定顺序打印文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!