Thursday 8 August 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...