shell - Parse output in python to count number of occurences -


i have python script makes curl command , reads output variable called output

the output similar this:

"endpoint1: {     "[service1]endpoint": {     },     "[service1]endpoint": {     },     "[service2]endpoint": {     },     "[service2]endpoint": {     }   } "endpoint2": {     "[service1]endpoint": {     }   }, 

what need parse output count unique strings between [] , print out occurrences. this:

service1 3  service2 2 

any on how go doing this. thanks.

if input isn't valid json, use following approach collections.counter object , re.findall() function:

result = collections.counter(re.findall(r'\[([^]]+)\]', output)) print(result) 

the output:

counter({'service1': 3, 'service2': 2}) 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -