简体   繁体   English

以awk的特定顺序打印文件

[英]Print file in particular order in awk

I am following this link https://stackoverflow.com/a/54599800/10220825 我正在关注此链接https://stackoverflow.com/a/54599800/10220825

file.txt file.txt的

Iteration 1
RAM: +2s
Cache: +142ms (total +417ms)

Iteration 2
RAM: +184ms
Cache: +172ms
Searchms: +131ms (total +385ms)

Iteration 3
RAM: +149ms
Searchms: +3.2s

I want to remove the ms or s from the time value but not from name (example it should not remove Searchms to Search). 我想从时间值中删除ms or s ,但不从名称中删除(例如,它不应删除Searchms到Search)。 Also I want to convert s into ms by multiply into 1000 if the time value contains s and print result accordingly. 另外,如果时间值包含s ,我想通过multiply into 1000s into ms转换s into ms并相应地打印结果。

Expected Output: 预期产量:

RAM: 2000 184 149
Cache: 142 172
Searchms: 131 3200

try.awk try.awk

/:/{vals[$1]=vals[$1] OFS $2+0}
END {
         for (key in vals)
          print key vals[key]
}

On executing : awk -f try.awk file.txt 执行时: awk -f try.awk file.txt

Code Output: 代码输出:

Cache: 142 172
Searchms: 131 3.2
RAM: 2 184 149

In my output s is not converting to ms. 在我的输出中, s is not converting to ms. Please suggest me how I modify above source code try.awk to convert s to ms. 请建议我如何修改上述源代码try.awk将s转换为ms。


New Test Case: file.txt 新的测试用例: file.txt

Iteration 1
RAM: +2s342ms

Iteration 2
RAM: +2s

Iteration 3
RAM: +149ms

Code: 码:

/:/ && $2 ~/ms$/{vals[$1]=vals[$1] OFS $2+0;next}
/:/ && $2 ~/[^m]s$/{vals[$1]=vals[$1] OFS ($2+0)*1000}
END {
         for (key in vals)
          print key vals[key]
}

Expected output: 预期产量:

RAM: 2342 2000 149

Output: 输出:

RAM: 2 2000 149

You can use the following awk script: 您可以使用以下awk脚本:

/:/ && $2 ~/ms$/{vals[$1]=vals[$1] OFS $2+0;next}
/:/ && $2 ~/[^m]s$/{vals[$1]=vals[$1] OFS ($2+0)*1000}
END {
         for (key in vals)
          print key vals[key]
}

This will produce the output: 这将产生输出:

awk -f try.awk file.txt 
Cache: 142 172
Searchms: 131 3200
RAM: 2000 184 149

Explanations: 说明:

  • The condition $2 ~/ms$/ will check if we have a line with ms in this case the same logic is done as before and the next will force awk to jump to the next line. 条件$2 ~/ms$/将检查如果我们有一个线ms在这种情况下,相同的逻辑作为之前完成和next将迫使awk跳转到下一行。
  • /:/ && $2 ~/[^m]s$/{vals[$1]=vals[$1] OFS ($2+0)*1000} when we reach this scope we know that we have a line with units in s and we multiply it by 1000 to convert it to ms . /:/ && $2 ~/[^m]s$/{vals[$1]=vals[$1] OFS ($2+0)*1000}当我们到达此范围时,我们知道我们有一条线,单位为s和我们将其乘以1000即可将其转换为ms

To answer to your new requirements, I have adapted the try.awk into: 为了满足您的新要求,我将try.awk为:

/:/ && $2 ~/[0-9]+s[0-9]+ms$/{split($2,buff,/s/);vals[$1]=vals[$1] OFS (buff[1]+0)*1000+buff[2];next}
/:/ && $2 ~/ms$/{vals[$1]=vals[$1] OFS $2+0;next}
/:/ && $2 ~/[^m]s$/{vals[$1]=vals[$1] OFS ($2+0)*1000}
END {
         for (key in vals)
          print key vals[key]
}

Input: 输入:

$ cat file2.txt 
Iteration 1
RAM: +2s342ms

Iteration 2
RAM: +2s

Iteration 3
RAM: +149ms

Output: 输出:

$ awk -f try.awk file2.txt 
RAM: 2342 2000 149

A small adaptation of the OP: OP的小改编:

/:/{  vals[$1]=vals[$1] OFS $2*($2~/ms$/ ? 1 : 1000 ) }
END { for (key in vals) print key vals[key] }

Remark: this will not work on all versions of awk, this makes use of the following nifty feature of gawk (and POSIX): 备注:这不适用于所有版本的awk,这利用了gawk(和POSIX)的以下出色功能:

A string is converted to a number by interpreting any numeric prefix of the string as numerals: "2.5" converts to 2.5, "1e3" converts to 1,000, and "25fix" has a numeric value of 25 . 通过将字符串的任何数字前缀解释为数字,可以将字符串转换为数字:“ 2.5”转换为2.5,“ 1e3”转换为1,000, “ 25fix”的数字值为25 Strings that can't be interpreted as valid numbers convert to zero. 无法解释为有效数字的字符串将转换为零。

source: Gnu Awk manual: Conversion of Strings and Numbers 来源: Gnu Awk手册:字符串和数字的转换

This behaviour is Posix valid and works using the C-function atof . 此行为是Posix有效的,并使用C函数atof起作用。 POSIX, however, gives a second option to convert strings to numbers where "25fix" would convert to 0. This is, for example, the case on Oracle: 但是,POSIX提供了将字符串转换为数字的第二个选项,其中“ 25fix”将转换为0。例如,在Oracle上就是这种情况:

Linux: GNU $ awk 'BEGIN{print "25fix"+0}'
25
SUN OS: SUNWesu $ /usr/bin/awk 'BEGIN{print "25fix"+0}'
0
SUN OS: SUNWxpg $ /usr/xpg4/bin/awk 'BEGIN{print "25fix"+0}'
25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM