【问题标题】:Extract a property value from a text file从文本文件中提取属性值
【发布时间】:2020-12-27 22:48:05
【问题描述】:

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

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

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

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

然而这会产生:

committed=1728469KB)

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

【问题讨论】:

标签: bash shell awk sed


【解决方案1】:
$ awk -F'[=)]' '{print $3}' file
1728469KB

【讨论】:

    【解决方案2】:

    您能否尝试使用awk 的match 函数来关注。

    awk 'match($0,/committed=[0-9]+/){print substr($0,RSTART+10,RLENGTH-10)}' Input_file
    

    使用 GNU grep 使用它的 \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.
    

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      str="Internal (reserved=1728469KB, committed=1728469KB)"
      echo $str | awk '{print $3}' | cut -d "=" -f2 | rev | cut -c4- | rev
      

      【讨论】:

        【解决方案4】:

        Sed 更擅长简单的匹配任务:

        sed -n 's/.*committed=\([0-9]*\).*/\1/p' input_file
        

        【讨论】:

          猜你喜欢
          • 2016-01-10
          • 1970-01-01
          • 1970-01-01
          • 2018-05-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多