首页 > 解决方案 > 从文本文件中提取属性值

问题描述

我有一个日志文件,其中包含如下行:

Internal (reserved=1728469KB, committed=1728469KB)

我需要提取“已提交”中包含的值,所以 1728469 我正在尝试使用 awk

cat file.txt | awk '{print $4}' 

然而,这会产生:

committed=1728469KB)

这仍然不完整,还需要一些工作。有没有更简单的解决方案来代替?谢谢

标签: bashshellawksed

解决方案


您能否尝试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.

推荐阅读