0

I have an html file which contains a line like this:

base_url = '',

Sometimes there may be nothing between the single quotes; sometimes there might be a url there. How do I write single line perl command which fills in the string with a base url?

I tried this, but it doesn't work:

perl -pe 's/(base_url = \')(.+)/https:\/\/www\.example\.com(\',)/eg' -i index.htm

Thanks

TLP
  • 66,756
  • 10
  • 92
  • 149
jacob
  • 2,762
  • 1
  • 20
  • 49
  • try to use this regexp: `/base_url = '([^']+)'/` – Iłya Bursov Oct 21 '13 at 21:11
  • @IlyaBursov `.*?` is to be preferred over `[^']*` (yes, it's `*` not `+`, since it can be zero-length). – C. K. Young Oct 21 '13 at 21:12
  • @ChrisJester-Young you're right – Iłya Bursov Oct 21 '13 at 21:13
  • What *exactly* are you trying to do? Is that an attribute of an element, or is it just text? Either way, you should be using an HTML parser… Please explain your actual goal, not about how a possibly bad solution might be used (see: [XY-Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)) – amon Oct 21 '13 at 21:30
  • 1
    Arnon - It's a line of javascript in an html file. I'm familiar with XY problem, but explaining my original problem opens my question to too broad a range of answers than I want to deal with right now...My problem is: I have a hybrid iOS application which contains an html, and I need to specify the Ajax server url which varies by build (dev, test, prod). I am putting the Perl command into a build phase script. ...Chris answered below concisely. – jacob Oct 21 '13 at 23:17

2 Answers2

3

Try this:

s|base_url = '.*?',|base_url = 'https://www.example.com/',|g;
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
-1

You asked for a one-liner but didn't say which shell you're using. This matters a lot. I'll assume bash. This is one way:

perl -p -e 's#(base_url *= *)'"'"'.*'"'"'#$1'"'"'https://www.example.com/'"'"'#'

As you can see, the hard part is escaping single quotes within single quotes. There may be a simpler way to do it, but I don't know what that is.

Some folks don't seem to like the single quote escape within single quotes convention. I like it because you don't have to worry about any further shell escapes. Here's an answer with double quotes:

perl -p -e "s#(base_url *= *)'.*'#\$1'https://www.example.com/'#"
Gene
  • 46,253
  • 4
  • 58
  • 96
  • In bash you can use `"` with Perl one-liners. – TLP Oct 21 '13 at 21:57
  • @TLP The double quote tries to expand $ and other shell metacharacters. So it has its own problems. – Gene Oct 21 '13 at 22:00
  • 3
    I think I would prefer to use backslashes instead of `'"'"'`. But you don't need that here, just use `\K` instead. Also, you cannot use both `-n` and `-p`. – TLP Oct 21 '13 at 22:12
  • `'"'"'` makes me feel this is too complex for a one liner – justintime Oct 21 '13 at 22:46
  • @TLP Well, I'm glad to know your preference. Can you give an expression that works with baskslashes? This SO article http://stackoverflow.com/questions/1250079/bash-escaping-single-quotes-inside-of-single-quoted-strings/1315213#1315213 shows that it's not so simple. I'm not familiar with \K. Can you give a reference? Thanks. – Gene Oct 21 '13 at 22:51
  • @TLP Forgot. Thanks for the -n -p thing. I had -n -i because OP originally had it that way. – Gene Oct 21 '13 at 23:08
  • [perldoc perlre](http://perldoc.perl.org/perlre.html#Character-Classes-and-other-Special-Escapes). `\K` will "keep" everything to the left of it, very straightforward. – TLP Oct 21 '13 at 23:36