How do I match the URL address in this string, I have other code that matches text and it seems to work, but when I try to use it here, it doesn’t, it keeps saying there is “No such file or directory. I didn’t know grep -o only worked on files?
matchString='url={"urlPath":"http://www.google.com/","thisIsOtherText"'
array=($(grep -o 'url={"urlPath":"([^"]+)"' "$matchString"))
grep: url={"urlPath":"http://www.google.com/","thisIsOtherStuff": No such file or directory
Anyway, could you please help me with matching the URL from in the “matchString” variable (it doesn’t have to use grep).
Preferred output: http://www.google.com/
You need to
echothe string through a pipe togrep:Grep reads from a file or standard input. It doesn’t accept a string argument to search within.
Also,
grepis going to output the entire match, not the part in parentheses. You probably need to usesed.The
sedcommand works like this:s///is the substitute command and its delimiters. You can use another delimiter for convenience if it makes the expression more readable or helps eliminate having to do some escapes. Between the first two delimiters is what we want to change. Between the middle one and the last one is what we want to change it to.url={"urlPath":"is just the literal text we are using to help make the match\( \)encloses a capture group. What falls bewteen here is what we want to snag.[^"]matches any character that’s not a double-quote\+match one or more of the preceding pattern. So, in this case, that’s one or more characters that are not quotation marks..*match zero or more of any character. In this case, it starts at the quote aftergoogle.com/and goes to the end of the string.\1outputs what was captured by the first (and only in this case) capture group.Visually:
url={"urlPath":" http://www.google.com/ ","thisIsOtherText" -----literal---- -------non-quote------ ---any character--- url={"urlPath":" \( [^"] \) .*