LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   escaping single quote with regex (https://www.linuxquestions.org/questions/linux-newbie-8/escaping-single-quote-with-regex-4175413424/)

ted_chou12 06-26-2012 03:35 AM

escaping single quote with regex
 
Hi, I am having trouble trying to get my intended results from this code:
Code:

torrentlink=($(sed -rn "/target=/s/.*href=\"([^\"]+)\".*>.*\.${vars[1]}\.torrent.*/\1/p" "$html"))
torrentlink=($(sed -rn "/target=/s/.*href=\"([^\"]+)\".*>.*\.${vars[1]}\.torrent.*/\1/p" "$html"))
I believe I am not correct in trying to escape the single quote, but there is an array defined term in the variable, so this makes the coding even more difficult.
Another question that I have is whether the . for file extension needed backslash to escape, because they seem to be working fine with or without backslashes.
THanks,
Ted

catkin 06-26-2012 04:17 AM

I haven't looked at the sed command (others here can do that better than I) but, assuming this is in a bash script,
Code:

torrentlink=($(sed <whatever>))
will not assign anything to variable torrentlink, it will simply (try to) run the output of the sed command in a subshell, writing the output to stdout which is probably the terminal.

Assuming you want the output of the sed command in variable torrentlink:
Code:

torrentlink=$(sed <whatever>)
What is the output of the sed command alone? For debugging:
Code:

sed <whatever>
torrentlink=$(sed <whatever>)


ted_chou12 06-26-2012 11:23 PM

Thanks, I solved by using both double and single quotes:
Code:

torrentlink=($(sed -rn '/target=/s/.*href="([^"]+)".*>.*\.'"${vars[1]}"'\.torrent.*/\1/p' "$html"))
This seem to work perfectly without errors, the point of adding double quotes for ${var[1]} in cases of space or tab characters in the variable.
Thanks,
Ted


All times are GMT -5. The time now is 07:48 PM.