Nimesh Doshi Nimesh Doshi - 5 months ago 12
Linux Question

to grep specific set of strings occuring multiple times in a single line

MY system generate very lengthy lines in logs of which i want to grep only few fields values. I tried with grep and sed and I'm nowhere close.

Here, Single log line contains multiple Channel fields and its analysis.
How to get a specific Channel (occurring more than once) from a single log line which contains many such channel information.
For e.g. here i need video channel analysis and it's occurring twice in line.


.......{"Channel":{"Type": "Video","Protocol": "DYNAMIC_96","Rate":
"1999102","Packets":{"Forwarded":{"Total": "570495","RTPAndRTCP":
"570495","KeepAlives": "0","Unknown": "0"},"KeepAlives":{"Total":
"0","Assent": "0","H460": "0","STUN": "0"},"Errors":{"Total":
"0","Media":{"NoDestination": "0","InvalidType": "0","FilterFailure":
"0","MediaDiscarded":
"0","Filter":{"SRTP":{"Decrypt":{"Authentication": "0","Decrypt":
"0","BadRTPHeader": "0","FECIgnored": "0","Other":
"0"}},"RTP":{"Payload":{"Rewrite":{"Unexpected":
"0"}}}}},"Assent":{"InvalidProbe": "0"},"IP":{"Send":
"0"}},"Analysis":{"Duplicate": "0","Lost": "4","OutOfOrder":
"1","Unexpected": "0","Jitter": "8","JitterPeak":
"48"}},"Incoming":{"Leg": "BSide"},"Outgoing":{"Leg":
"ASide"}}},.....................{"Channel":{"Type":
"Video","Protocol": "DYNAMIC_96","Rate":
"1617864","Packets":{"Forwarded":{"Total": "407214","RTPAndRTCP":
"407214","KeepAlives": "0","Unknown": "0"},"KeepAlives":{"Total":
"0","Assent": "0","H460": "0","STUN": "0"},"Errors":{"Total":
"0","Media":{"NoDestination": "0","InvalidType": "0","FilterFailure":
"0","MediaDiscarded": "0","Filter":{"SRTP":{"Encrypt":{"Encrypt":
"0","BadRTPHeader": "0","FECIgnored": "0","DatagramSizeExceeded":
"0","Other": "0"}},"RTP":{"Payload":{"Rewrite":{"Unexpected":
"0"}}}}},"Assent":{"InvalidProbe": "0"},"IP":{"Send":
"0"}},"Analysis":{"Duplicate": "0","Lost": "1045","OutOfOrder":
"0","Unexpected": "957","Jitter": "3","JitterPeak":
"1143"}},"Incoming":{"Leg": "ASide"},"Outgoing":{"Leg":
"BSide"}}},......


I am interested only in above these 2 channel analysis and to throw all blah blah (.....) information.

Any pointers will be of great help. Thanks !!

Answer

To get the channels from the line you can use:

grep -Eo '"Channel":{[^{]+({[^}]+})+}'

This will output for your example:

"Channel":{"Type": "Video","Protocol": "DYNAMIC_96","Rate": "1999102","Packets":{"Forwarded":{"Total": "570495","RTPAndRTCP": "570495","KeepAlives": "0","Unknown": "0"},"KeepAlives":{"Total": "0","Assent": "0","H460": "0","STUN": "0"},"Errors":{"Total": "0","Media":{"NoDestination": "0","InvalidType": "0","FilterFailure": "0","MediaDiscarded": "0","Filter":{"SRTP":{"Decrypt":{"Authentication": "0","Decrypt": "0","BadRTPHeader": "0","FECIgnored": "0","Other": "0"}}
"Channel":{"Type": "Video","Protocol": "DYNAMIC_96","Rate": "1617864","Packets":{"Forwarded":{"Total": "407214","RTPAndRTCP": "407214","KeepAlives": "0","Unknown": "0"},"KeepAlives":{"Total": "0","Assent": "0","H460": "0","STUN": "0"},"Errors":{"Total": "0","Media":{"NoDestination": "0","InvalidType": "0","FilterFailure": "0","MediaDiscarded": "0","Filter":{"SRTP":{"Encrypt":{"Encrypt": "0","BadRTPHeader": "0","FECIgnored": "0","DatagramSizeExceeded": "0","Other": "0"}}
Comments