2 Replies - 2155 Views - Last Post: 29 June 2013 - 08:08 AM Rate Topic: -----

#1 GetOffMyTaco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-June 13

Bash Script Regex With Variables

Posted 27 June 2013 - 12:07 PM

LoginStatus=`curl -s $site | sed -En 's/.*($building,$roomparse,.*Status=)[^0-9]*([0-9.]+).*/\2/p'`


Hi all, I'm having trouble with this regex expression because it has variables in it. After some Google I've tried using single quotes around the variables such as this:

LoginStatus=`curl -s $site | sed -En 's/.*('$building','$roomparse',.*Status=)[^0-9]*([0-9.]+).*/\2/p'`


This errors out after the first word in the variable:

"sed: 1: "s/.*(West": unterminated substitute pattern"

When the output of that variable should be "West Lane". Does anyone know how I should go about doing this? Thanks for any assistance you can provide.

Is This A Good Question/Topic? 0
  • +

Replies To: Bash Script Regex With Variables

#2 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Bash Script Regex With Variables

Posted 29 June 2013 - 06:47 AM

I'm not sure, but I'd evaluate the strings first before giving them to sed. The sed-command is "heavy-quoted" to not let shell to do anything with it.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Bash Script Regex With Variables

Posted 29 June 2013 - 08:08 AM

As you know, you can't use variables inside single quotes, which is why you put them outside of the quotes. But the problem with not quoting them at all is that now any spaces within the variables are not escaped, so the shell splits them into multiple arguments at the spaces.

To fix your problem, you need to put double quotes around your variables. You can either do this by mixing single quotes and double quotes or you could use double quotes around the whole thing and simply use backslashes to escape any characters of the regex that need escaping within double quotes, which in this case is none of them as far as I can tell. So this should work:

sed -En "s/.*($building,$roomparse,.*Status=)[^0-9]*([0-9.]+).*/\2/p"


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1