Executing a shell script containing a "¶"

In my awk program I do a global replace of “¶” with “\n”. When I type this in on the command line it works fine. When I type this into AppleScript:


do shell script "awk 'BEGIN {text=\"hello¶there\"; gsub(\"¶\", \"\\n\", text); print text}'"

It also works fine. However if I put “hello¶there” into a file test.txt and then run this script:


do shell script "awk '{text=$0; gsub(\"¶\", \"\\n\", text); print text}' test.txt"

It does not work! I’m thinking maybe this has something to do with text encoding or something, but I can’t figure it out. Can anyone shed any light on this?

I did some investigating and found that AppleScript stores string variables with MacRoman encoding. Since my text file containing the “¶” is also MacRoman-encoded (saved with TextEdit) I wouldn’t think there would be a problem. However, maybe the reason it doesn’t work is that the shell uses some other encoding and the conversion gets screwed up somewhere.

So the solution is to save the file with Unicode encoding (UTF-8 or UTF-16) and then change the script to this:


do shell script ("awk '{text=$0; gsub(\"" & "¶" as Unicode text & "\", \"\\n\", text); print text}' test.txt")

This works fine.