0

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.

  • 1
    Possible duplicate of [How to use grep to match multiple strings?](http://stackoverflow.com/questions/4487328/how-to-use-grep-to-match-multiple-strings) – Benjamin W. Jan 31 '16 at 09:24

1 Answers1

1

Or use alternation metacharacter:

grep -oP 'name\\":\\"[^\\]*\\|id\\":[^,]*' | tr -d '\\"'
                              ↑
          |__________________| |____________|
             look for this   or    that
Maroun
  • 94,125
  • 30
  • 188
  • 241