-1

I got this input and I try to extract "out_of_stock". I want to extract the value between "code":" and ","days_. Consider "{"id":5,"text":"Stoc epuizat","code":"out_of_stock","days_estimation":0" as any other string ignore the json appearace.. Also I would like to assign it to a variable in bash

$ cat input_test.html
availability: {"id":5,"text":"Stoc epuizat","code":"out_of_stock","days_estimation":0
$ sed -n "s/.*code\":\"(.*)\",\"days.*/\1/p" input_test.html > output_test.html
sed: -e expression #1, char 29: invalid reference \1 on `s' command's RHS
Andrei
  • 37
  • 1
  • 6

1 Answers1

-1

If out_of_stock you want, just:

$ grep -o out_of_stock input_test.html
out_of_stock

Edit: Using grep and PCRE lookaround (where available):

$ grep -oP "(?<=\"code\":\").*(?=\",\"days_)" input_test.html
out_of_stock
James Brown
  • 36,089
  • 7
  • 43
  • 59
  • This doesn't help me. The value may differ I would like to extract what is between code":" and ","days and assign it to a variable – Andrei Dec 16 '20 at 21:19
  • Thank you for asking a proper question. Updated my answer. – James Brown Dec 16 '20 at 21:37
  • I'd assume that the downvoter saw the first version and hasn't been here to notice the updates since. – Charles Duffy Dec 16 '20 at 21:39
  • It was downvoted after the edit. But anything for a good show and tell. – James Brown Dec 16 '20 at 21:40
  • Your attitude says a lot about the downvoting, but I think you may try to focus on giving a proper answer too. How does your regular expression work? The way it is presented now does not help other people. – Rfroes87 Dec 16 '20 at 21:41
  • 1
    It was a proper answer to the given question. What comes to the standing answer, _lookaround_ and _PCRE_ should tell everything and if not, they are great catchwords to google. – James Brown Dec 16 '20 at 21:43