I use see quite often in shell scripts but have just run into an issue of escaping a backslash in a sed string:
If I don’t put a backslash in from of the “\a”, AS complains and won’t compile.
If I do put one to escape the “\a”, AS compiles but sed fails.
Can anyone tell me how I might work around this?
This works:
This fails to compile:
This compiles but causes a sed failure:
Error is (Script Debugger won’t display entire message):
Browser: Safari 537.76.4
Operating System: Mac OS X (10.8)
Hi,
normally a second backslash is needed to escape the first one
do shell script "sed '/Sunrise/ a\\Add this line after every line with Sunrise'"
You could also try
set theText to "/Sunrise/ a\\Add this line after every line with Sunrise"
do shell script "sed " & quoted form of theText
quoted form of uses always the best way to escape the phrase
Good suggestion. Here is what happens:
please look at my example, quoted form of is not part of the literal string
You’re right, I wasn’t careful.
Like this then?
add a space character after sed
Here’s what I get:
I’m beginning to wonder if I have the basic sed command wrong. I’ll dig into that.
Hi.
The ‘a’ command should be followed by both an escaped backslash and a linefeed. And if you want to insert a line rather than just prepend it to the following one, the inserted text should be followed by a linefeed too. Assuming the first part of your shell script works OK, it would look like this:
set sunriseLines to do shell script "/usr/local/bin/tide -em p -tf \"%H:%M %Z\" -l \"" & theTideStationChosen & "\" | sed '/Sunrise/ a\\'$'\\n''Add this line after every line with Sunrise'$'\\n'''"
Or slightly more simply:
set sunriseLines to do shell script "/usr/local/bin/tide -em p -tf \"%H:%M %Z\" -l \"" & theTideStationChosen & "\" | sed '/Sunrise/ a\\\nAdd this line after every line with Sunrise\n"
Brilliant Nigel, thank you. I have a lot more to learn about those *nix utilities!
Browser: Safari 537.76.4
Operating System: Mac OS X (10.8)