-2

I am executing below command to get particular string from json output , here I am grepping with string "uri" which prints all the required fields.

# curl -X GET --insecure -H "$(cat /token/.bearer_header)" http://localhost:3000/api/search?query=% |  sed -e 
's/[{}]/''/g' | awk -v RS=',"' -F: '/^uri/ {print $2}' 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2410    0  2410    0     0  40166      0 --:--:-- --:--:-- --:--:-- 40847
"ab/any-dashboard"
"ab/many-dashboard"
"ab/too-dashboard"
"ab/maximum-dashboard"
"ab/minimum-dashboard"

now I want to replace for example "ab/any-dashboard" to only any-dashboard, which means above command should print like below

any-dashboard
many-dashboard
too-dashboard
maximum-dashboard
minimum-dashboard

can anyone help me here I tried several things with sed but not getting exact result, and I dont have knowledge in JQ.

Regards, SAM

1
  • 3
    Edit your question with the actual JSON being emitted instead of what remains after mangling with sed and awk
    – Botje
    Commented Oct 20, 2021 at 13:54

2 Answers 2

2

This might work for you (GNU sed):

sed -nE 's#".*/(.*)"#\1#p' file

Turn off implicit printing -n, and turn on extended regexp -E (-r on some platforms).

Pattern match on the required string and replace it with the expected part and print the result.

N.B. The (...) in the LHS of the substitution command is called a back reference and can be recalled in the RHS. Also the use of # as an alternative separator rather than the usual / which is part of the match.

1
  • this was the exact requirement and thank you for your explanation this will surely help me for future.
    – Samurai
    Commented Oct 20, 2021 at 16:22
0

If the awk statement below (which requires GNU awk for multi-char RS) is at the end of the command line, try adding this gsub function and see if you get what you want:

Current:
awk -v RS=',"' -F: '/^uri/ {print $2}'

Change to:

awk -v RS=',"' -F: '/^uri/ {gsub("/","-",$2);print $2}'
1
  • Potong has shared the exact command i was looking for, even your solution is very close to my requirement but it was not removing double quotes, thank you for you time and help :)
    – Samurai
    Commented Oct 20, 2021 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.