【问题标题】:Grep numbers between colon and comma冒号和逗号之间的 Grep 数字
【发布时间】:2013-05-27 06:12:13
【问题描述】:

我想 grep 包含超过 70% 使用率的所有结果

输出示例:

{"ipaddr":"1.1.1.1","hostname":"host1.test.com","percentage":69,"dir":"/root"},
{"ipaddr":"1.1.1.1","hostname":"host1.test.com","percentage":79,"dir":"/oracle"},
{"ipaddr":"1.1.1.1","hostname":"host1.test.com","percentage":1,"dir":"/oradump"},
{"ipaddr":"1.1.1.1","hostname":"host1.test.com","percentage":90,"dir":"/archive"},

grep 后的预期视图:

{"ipaddr":"1.1.1.1","hostname":"host1.test.com","percentage":79,"dir":"/oracle"},
{"ipaddr":"1.1.1.1","hostname":"host1.test.com","percentage":90,"dir":"/archive"},

【问题讨论】:

    标签: perl bash sed awk grep


    【解决方案1】:

    Awk 更适合这里:

    $ awk -F'[:,]' '$6>70' file
    {"ipaddr":"1.1.1.1","hostname":"host1.test.com","percentage":79,"dir":"/oracle"},
    {"ipaddr":"1.1.1.1","hostname":"host1.test.com","percentage":90,"dir":"/archive"},
    

    【讨论】:

    • +1 为这项工作推荐合适的工具。这是为 awk 量身定做的
    【解决方案2】:

    或者使用 Perl:

    $ perl -ne'print if /"percentage":([0-9]+),/ and $1 > 70'
    

    (不需要讨厌的分隔符计数)

    【讨论】:

      【解决方案3】:
      perl -F'[:,]' -ane 'print if $F[5]>70' file
      

      【讨论】:

        【解决方案4】:

        GNU sed

        sed -n '/:[0]\?70,/d;/:[0-1]\?[7-9][0-9],/p' file
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多