bash - 从文本文件中提取属性值
问题描述
我有一个日志文件,其中包含如下行:
Internal (reserved=1728469KB, committed=1728469KB)
我需要提取“已提交”中包含的值,所以 1728469 我正在尝试使用 awk
cat file.txt | awk '{print $4}'
然而,这会产生:
committed=1728469KB)
这仍然不完整,还需要一些工作。有没有更简单的解决方案来代替?谢谢
解决方案
您能否尝试match
使用awk
.
awk 'match($0,/committed=[0-9]+/){print substr($0,RSTART+10,RLENGTH-10)}' Input_file
使用 GNUgrep
使用\K
它的选项:
grep -oP '.*committed=\K[0-9]*' Input_file
输出将1728469
在上述两种解决方案中。
第一种解决方案说明:
awk ' ##Starting awk program from here.
match($0,/committed=[0-9]+/){ ##Using match function to match from committed= till digits in current line.
print substr($0,RSTART+10,RLENGTH-10) ##Printing sub string from RSTART+10 to RLENGTH-10 in current line.
}
' Input_file ##Mentioning Input_file name here.
推荐阅读
- angular - 即使在活动选项卡上,Angular Material 选项卡也会变灰
- java - 如何使用 Java 9+ 修改 AST
- sas - 使用宏在数据集中按组对列求和
- python - 有没有办法断言两个字符串除了python中的一个部分之外是相同的?
- python - 用另一个相同键行中的非 NA 值填充键控行中的 NA 列值
- python - 我能在下面做什么,我被卡住了?
- talend - 尝试在 Pivtal Cloud FOundry 中部署 Talend Agent 时失败
- typescript - 如何在 Typescript 中修复这种类型推断
- vba - 如何更改此代码,以便任务调度程序可以将其作为 .vbs 文件运行?
- c++ - 在 OpenCV 中访问具有多个线程的 Mat 是否是线程安全的?