我有一个包含以下内容的大文件(这是它的摘录):
Total execution time (hh:mm:ss.ms): 00:00:8,4880 (8488 mseconds)
Error 0.00044
Stage 1 execution time (hh:mm:ss.ms): 00:00:7,0430 (7043 mseconds)
Stage 2 execution time (hh:mm:ss.ms): 00:00:1,4450 (1445 mseconds)
Total execution time (hh:mm:ss.ms): 00:00:0,0170 (17 mseconds)
Number of variables 6
这些行可能不止一次并且具有不同的值。我想替换 mseconds 值。所以,预期的输出是:
8488
Error 0.00044
7043
1445
17
Number of variables 6
我试过这个,但我不知道如何替换该行中提取的值:
grep "Total execution time (hh:mm:ss.ms):" file.txt | cut -d " " -f6 | cut -d "(" -f2
而且我还考虑为每个案例(Total、Stage 1 和 Stage 2)都这样做,但如果有更实用的东西,我将非常感激。
使用sed
(我使用 GNU sed,具体来说)来实现结果会更容易:
sed -E '/execution time/s/.*\(([0-9]*) mseconds\)/\1/'
仅将数字部分保留\(([0-9]*) mseconds\)
在带有 pattern 的行中execution time
。的([0-9]*)
是通过使用用作反向引用,以便输出所需的数目\1
,并且\(
与\)
平均字面括号。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句