user3207650 user3207650 - 6 months ago 21
Linux Question

Grep certain characters

So, in the following string:

"body":"MESSAGE"


I am trying to just extract the word MESSAGE. I have been playing around with grep trying to figure it out, but can now only get the first letter M to print.

grep -oP "body":"\K[^"]+,]


I tried using the above statement, but cant seem to get the whole word MESSAGE without quotes separated. This string MESSAGE changes, so the grep statement just has to be able to get the text in between "body":" and ".

Any help would be greatly appreciated!

Answer

This should work:

grep -oP '"body":"\K[^"]+' <<< '"body":"SOME MESSAGE"'

SOME MESSAGE