Baskar Jayakumar
Baskar Jayakumar

Reputation: 11

grep multiple pattern from single file and store output on separate files for each pattern

I have a script to pull metrics of various params from jmx.out file. Is there a way to implement the logic in single command to avoid reading jmx.out file everytime?

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

Upvotes: 0

Views: 950

Answers (2)

mr.tee
mr.tee

Reputation: 35

you can do

sed -n -e "/CallQueueLength/w call_queue_length.out" -e "/RpcProcessingTimeAvgTime/w rpc_avg_time.out" ...

you welcome

Upvotes: 1

blackSmith
blackSmith

Reputation: 3154

I'm not able to give a solution with a single command, but following may be helpful :

At first combine all the required pattern assign it to a single variable (it's just for readability) :

pattern="RpcProcessingTimeAvgTime|DeleteNumOps|CreateFileOps|GetFileInfoNumOps"

Now loop through the grep output and redirect the matched line to a file which name will be the current match omitting other characters :

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

However there is limitation of it. When a line contains multiple matches, it will go to the file named by the pattern which is encountered first. Removing the head -1 part solves this problem, but the filename won't be appropriate then if a single line contains same pattern multiple times.

Upvotes: 0

Related Questions