0

Apologize for such dummy question but this is my first time using curl command and now I got this command from somewhere to extract the following string

{"success":true,"results":1,"total":1,"more":false,"offset":0,"hits":[{"path":"/home/users/Vq7DPVRHzGVK--OTJsHs","excerpt":"","name":"Vq7DPVRHzGVK--OTJsHs","title":"Vq7DPVRHzGVK--OTJsHs","lastModified":"2017-03-03 16:45:46","created":"2017-03-03 16:45:46"}]}

I pipe the curl output to sed with the following script:

sed -e 's/^.*"path":"\([^"]*\)".*$/\1/

Result:

/home/users/Vq7DPVRHzGVK--OTJsHs

Can anyone explain how's the regex work here? and how do I get the result for only Vq7DPVRHzGVK--OTJsHs instead of including the /home/user path?

2 Answers 2

2

Explanation:

s/   ^.*"path":"\([^"]*\)".*$   /  \1   /
      ----------^------------     ---^---
             Pattern         Replacement string 

How does Regular Expression work:

^.*         # Match beginning of input string & anything else
"path":"    # Up to literal string `"path":"`
\([^"]*\)   # Then match slash and match + group anything up to a double quote `"`
".*$        # Match double quote and the rest of input string

By replacement string \1 you are replacing whole matching part with first capturing group which is every thing between double quotes of path value except the beginning slash.

What you want is changing capturing group from capturing whole part to last section:

s/^.*"path":"[^"]*\/\([^"]*\)".*$/\1/
Sign up to request clarification or add additional context in comments.

2 Comments

@SLePort I am doing all right except the appreciation and compliment to revo, right?
@DEN Sorry, too quick comment. I didn't see you were an experienced user...
0

Regex demo

Regex: .*"path\":"\K[\/\w]+(?=\/)\/\K[^"]+

3 Comments

i got this error in return sed: -e expression #1, char 39: unterminated `s' command
SED doesn't support PCRE.
Welcome @DEN :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.