2016-04-27 90 views
0

我有一个脚本来从jmx.out文件中提取各种参数的度量。有没有办法在单一命令中执行逻辑以避免每次读取jmx.out文件? ?grep从单个文件的多个模式和存储在每个模式的单独文件的输出

curl -s "server.hostname.com:12345/jmx"   > jmx.out 
cat jmx.out | grep -E "CallQueueLength"   >> call_queue_length.out 
cat jmx.out | grep -E "RpcProcessingTimeAvgTime" >> rpc_avg_time.out 
cat jmx.out | grep -E "DeleteNumOps"    >> DeleteNumOps.out 
cat jmx.out | grep -E "CreateFileOps"   >> CreateFileOps.out 
cat jmx.out | grep -E "GetFileInfoNumOps"  >> GetFileInfoNumOps.out 
+0

文件名可以更改吗?你有超过5个参数阅读? –

回答

0

我不能给用一个命令一个解决方案,但是下面可能会有所帮助:

首先将所有需要的模式把它分配给一个变量(它只是可读性):

pattern="RpcProcessingTimeAvgTime|DeleteNumOps|CreateFileOps|GetFileInfoNumOps" 
通过grep的输出

现在环路和重定向匹配线到一个文件,该文件名称将是当前匹配省略其他字符:

grep -E $pattern jmx.out | while read line 
          do fileName=`echo $line | grep -oE $pattern | head -1` 
          echo $line >> $fileName 
          done 

但是有限制。当一行包含多个匹配时,它将转到由首先遇到的模式命名的文件。删除head -1部分解决了这个问题,但文件名将不合适,那么如果单个行多次包含相同的模式。

相关问题