认识grep -e, tr, xargs

来源:互联网 发布:淘宝交易货款延期延长 编辑:程序博客网 时间:2024/06/05 19:53
今天浏览shell代码发现了下面一行代码:
hey -m PUT -n 850 -d value=`head -c $keysize < /dev/zero | tr '\0' '\141'` -c 85 -T application/x-www-form-urlencoded $i/v2/keys/foo | grep -e "Requests/sec" -e "Latency" -e "90%" | tr "\n" "\t" | xargs echo &

从代码看,是对程序hey的结果进行格式化,下面看看是如何做的。

首先看看hey程序的结果:
$ hey -n 25600 -c 256 http://10.1.226.203:2379/v2/keys/foo
Summary:
  Total: 0.5229 secs
  Slowest: 0.0638 secs
  Fastest: 0.0001 secs
  Average: 0.0049 secs
  Requests/sec: 48957.8739
  Total data: 8934400 bytes
  Size/request: 349 bytes
Detailed Report:
DNS+dialup:
  Average: 0.0001 secs
  Fastest: 0.0000 secs
  Slowest: 0.0428 secs
DNS-lookup:
  Average: 0.0000 secs
  Fastest: 0.0000 secs
  Slowest: 0.0000 secs
Request Write:
  Average: 0.0001 secs
  Fastest: 0.0000 secs
  Slowest: 0.0213 secs
Response Wait:
  Average: 0.0038 secs
  Fastest: 0.0001 secs
  Slowest: 0.0190 secs
Response Read:
  Average: 0.0009 secs
  Fastest: 0.0000 secs
  Slowest: 0.0366 secs
Status code distribution:
  [200] 25600 responses
Response time histogram:
  0.000 [1] |
  0.007 [19621] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
  0.013 [5310] |∎∎∎∎∎∎∎∎∎∎∎
  0.019 [440] |∎
  0.026 [64] |
  0.032 [31] |
  0.038 [41] |
  0.045 [53] |
  0.051 [9] |
  0.057 [22] |
  0.064 [8] |
Latency distribution:
  10% in 0.0015 secs
  25% in 0.0025 secs
  50% in 0.0038 secs
  75% in 0.0062 secs
  90% in 0.0097 secs
  95% in 0.0114 secs
  99% in 0.0176 secs
$

再看看执行grep -e执行后的结果:
$ hey -n 25600 -c 256 http://10.1.226.203:2379/v2/keys/foo | grep -e "Requests/sec" -e "Latency" -e "90%"
  Requests/sec: 47604.4368
Latency distribution:
  90% in 0.0103 secs
$

原来grep命令里 -e 选项具有筛选多个字段的功能。

再看执行tr命令后的结果:
[david@localhost3 etcd]$ hey -n 25600 -c 256 http://10.1.226.203:2379/v2/keys/foo | grep -e "Requests/sec" -e "Latency" -e "90%"  | tr "\n" "\t" 
  Requests/sec: 48666.5181Latency distribution:  90% in 0.0095 secs[david@localhost3 etcd]$ 
[david@localhost3 etcd]$ 

原来tr命令的功能是 :替换
把所有的"\n" 替换为 "\t"

然后使用xargs把替换后的字符串打印出来。


原创粉丝点击