Thursday, August 8, 2019

regex - Grep for multiple words in log files

I have file which has contents like:



--------------------------------
service=serviceX
time=100ms
PARAMS={"data"=>"{\"id\":1,\"items\":[{\"quantity\":1,\"name\":\"itemName1\"

,\"quantity\":2,\"name\":\"itemName2\"}]}
------------------------------------------------
--------------------------------
service=serviceX
time=200ms
PARAMS={"data"=>"{\"id\":2,\"items\":[{\"quantity\":2,\"name\":\"itemName3\"
,\"quantity\":2,\"name\":\"itemName4\"}]}
------------------------------------------------
--------------------------------
service=serviceX

time=300ms
PARAMS={"data"=>"{\"id\":3,\"items\":[{\"quantity\":1,\"name\":\"itemName5\"
,\"quantity\":2,\"name\":\"itemName6\"}]}
------------------------------------------------


I am trying to find all the items in listed in the file.
My query looks like



grep -o  'name\\":\\"[^\\]*\\"' | tr -d '\\"'



and the output is



name:itemName1
name:itemName2
name:itemName3
name:itemName4
name:itemName5
name:itemName6



I want the output like:



id:1
name:itemName1
name:itemName2
id:2
name:itemName3
name:itemName4

id:3
name:itemName5
name:itemName6


I am new to linux and not sure how to grep for multiple words in a single query.

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...