4

我想 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"},
4

4 回答 4

7

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"},
于 2013-05-31T12:15:46.747 回答
3
perl -F'[:,]' -ane 'print if $F[5]>70' file
于 2013-05-31T12:27:39.110 回答
3

或者使用 Perl:

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

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

于 2013-05-31T12:18:12.700 回答
0

GNU sed

sed -n '/:[0]\?70,/d;/:[0-1]\?[7-9][0-9],/p' file
于 2013-06-01T22:47:37.307 回答