0

I was wondering if somebody could help me with this sed regex expression.

{"ok":false,"error":"invalid_arguments","response_metadata":{"messages":["[ERROR] missing required field: channel "]}}

From the following json, I need to extract the value corresponding to ok. Here it is false.

To do this, I use the sed -n '/"ok":[\w]*/p' regex but it doesn't work..

I'm new the sed and regex expression.. Is there somebody to help me??

Ga.R
  • 65
  • 1
  • 5
  • 1
    `sed -E 's/\{"ok":([a-zA-Z]+),.*/\1/' file` – User123 Jun 03 '21 at 06:11
  • Wow.. It works.. I'll study from now on. Thank you so much!! – Ga.R Jun 03 '21 at 06:12
  • I've posted this as an answer @Ga.R, if this has solution has worked for you, then you can upvote n accept it as an answer...happy learning :-) – User123 Jun 03 '21 at 06:35
  • Your attempt would simply print the entire line if it contains `"ok":` anywhere, except `sed` typically does not support the `\w` escape, so it was probably interpreted as a literal `w` and so the expression was not found. – tripleee Jun 03 '21 at 06:40
  • 1
    @User123 Thank you for answering! I want to upvote you, but my reputation is not enough.. I'll increase my repuation and come back soon :) – Ga.R Jun 03 '21 at 10:42
  • @tripleee Yes, I think you're right.. Thank you!! – Ga.R Jun 03 '21 at 10:42

1 Answers1

0

Using GNU sed:

sed -E 's/^\{"ok":([a-zA-Z]+),.*/\1/' file

Explanation:

-E              :: Use extended regular expressions rather than basic regular expressions
^\{"ok":        :: search for the string starting with: {"ok":
([a-zA-Z]+),.*  :: to capture the desired group using (), here ,the string: *false*
\1              :: backreference to print/output the captured group
User123
  • 1,498
  • 2
  • 12
  • 26