0

I want to use "/" (forward slash) in string in script but I am getting following error.

I have following applescript to replace content in file.

Syntax: do shell script "perl -pi -e 's/oldtext/newtext/g' " & quoted form of (POSIX path of file)

Following code is working because there is not special symbol.

do shell script "perl -pi -e 's/myname/yourname/g' " & quoted form of (POSIX path of file_tgt)

But following code is not working as it has "/".

do shell script "perl -pi -e 's/</b>/&lt;/b&gt;/g' " & quoted form of (POSIX path of file_tgt)

above code contain </b> throws following error:

Expected “"” but found unknown token.

if I add <\/b>, it throw following error.

Unknown regexp modifier "/b" at -e line 1, at end of line
Search pattern not terminated at -e line 1.

Can anybody please suggest a solution to me?

William Pursell
  • 204,365
  • 48
  • 270
  • 300
Nanji Mange
  • 2,155
  • 4
  • 29
  • 63
  • `do shell script "perl -pi -e 's//</b>/g' " & quoted form of (POSIX path of file_tgt)`. You still have one `/` at `/b`. Try escaping that one as well. – Justplayit94 Sep 07 '16 at 10:39
  • And maybe the one at `/g` as well. – Justplayit94 Sep 07 '16 at 10:39
  • @Justplayit94 I appologies but I am not getting your meaning. Can you please modify the line of `do shell script......`? – Nanji Mange Sep 07 '16 at 10:59
  • `do shell script "perl -pi -e 's/<\/b>/<\/b>\/g' " & quoted form of (POSIX path of file_tgt)` – Justplayit94 Sep 07 '16 at 11:00
  • @Justplayit94 I am getting this error `Expected “"” but found unknown token.` It highlights "/" symbol in `<\/b>`. Please suggest – Nanji Mange Sep 07 '16 at 11:03
  • @Justplayit94 This code is working because it doesn't have "/". do shell script "perl -pi -e 's//<b>/g' " & quoted form of (POSIX path of file_tgt) – Nanji Mange Sep 07 '16 at 11:06
  • Why are you trying to do this? It looks like you're writing to escape HTML. There are already ways to do this, e.g. https://metacpan.org/pod/HTML::Escape – Tom Fenech Sep 07 '16 at 11:16
  • Or he has to surround it like this : `""` – Justplayit94 Sep 07 '16 at 11:17
  • 1
    @Justplayit94 It's done. I need to replace `` with `<\\/b>` and it worked. Thank you for your efforts. – Nanji Mange Sep 07 '16 at 11:31
  • Seriously? Nice job! – Justplayit94 Sep 07 '16 at 11:40
  • Please post your answer so that it help other people in the future. – Justplayit94 Sep 07 '16 at 11:41
  • 1
    @TomFenech: Yup. [See also.](http://weblogs.asp.net/alex_papadimoulis/408925) What are the chances OP's AppleScript is dozens of lines of `do shell script` commands, each one substituting a single hardcoded HTML tag? OP should consider amending his original question to describe _what_ he wants to achieve (e.g. "How do I escape all HTML tags in text…") and _why_ (e.g. "so I can show raw HTML code in a webpage?"), not just _how_ he's currently trying to do it (which might be appropriate to his particular problem, or the worst conceivable way to do it – but we can't tell which without more info). – foo Sep 07 '16 at 12:51

1 Answers1

0

Here is the answer.

do shell script "perl -pi -e 's/<\\/b>/&lt;\\/b&gt;/g' " & quoted form of (POSIX path of file_tgt)
Nanji Mange
  • 2,155
  • 4
  • 29
  • 63