• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Q: echo | sed -- Solved!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Q: echo | sed -- Solved!


  • Subject: Re: Q: echo | sed -- Solved!
  • From: Christopher Nebel <email@hidden>
  • Date: Tue, 18 May 2004 15:20:43 -0700

On May 18, 2004, at 11:57 AM, Gnarlodious wrote:

Aargh! This \n vs \r line ending thing gives me a headache!
How I wish Apple had done away with \r when adopting Unix.

Way too much previous history. What would have solved it was using \n to begin with, but it's far too late for that unless you happen to have a wayback machine and a big bat handy.

Entity Nigel Smith spoke thus:

On your replaceLines, using Mac delimiters you are echoing a single line into sed, which replaces the 2 with the 8, spits it back out as a single line, which your editor then displays as multiple lines because of the Mac line breaks.

OK, here's the deal, which you've all gotten pretty close to:

First: two different line ending conventions here, \n (Unix-traditional) and \r (Mac-traditional). Most Unix tools -- including sed(1) -- only regard \n as a line break. When you hit return in a script, which one you get depends on the editor -- Script Editor gives you \n, SD gives you \r. Since they display the same, it's hard to tell them apart. (There's an open bug to start displaying both as escape sequences instead, so at least it would be unambiguous.)

Now then, here's what happens:

set x to "1\r2\r3\r4"
do shell script "echo " & x
--> "1
2
3
4"

Since sh(1) considers \r on par with a letter, x gets interpreted as a single argument, which gets passed to echo(1). No problem. However...

set x to "1\n2\n3\n4"
do shell script "echo " & x
--> AppleScript Error
sh: line 2: 2: command not found
sh: line 3: 3: command not found
sh: line 4: 4: command not found

In this case, sh saw \n, which is a statement separator, so it saw four commands: "echo 1", "2", "3", and "4". Naturally, it couldn't make much sense of the last three, hence the error. Quoting x (as in 'do shell script "echo " & quoted form of x') avoids this problem.

That's the echo(1) part, now on to sed. Again, sed applies its commands to "lines", where a line is defined by \n characters -- \r doesn't figure into it. Therefore:

set x to "1\r2\r3\r4"
do shell script "echo " & quoted form of x & " | sed 's/2/3/'"
--> "1
3
3
4"

Notice, \r in the string, but the command worked, because all it's asking to do is substitute "3" for the first "2" -- where the line breaks are (or in this case, aren't, at least from sed's perspective) doesn't matter. Now consider the other command:

set x to "1\r2\r3\r4"
do shell script "echo " & quoted form of x & " | sed '/2/,/3/d'"
--> ""

This time, you're asking sed to delete from the first line that contains "2" to the next line (including the current one) that contains "3". However, according to sed, the input only has one line, and it matches, so out it goes. In other words, sed worked, but it didn't do what you were expecting. To fix this, you'd need to translate the line endings to the \n that sed will respect:

set x to "1\r2\r3\r4"
do shell script "echo " & quoted form of x & " | tr '\\r' '\\n' | sed '/2/,/3/d'"
--> "1
4"

Evidently Script Editor and Smile convert the \r characters to \n along with "quoted form of" but Script Debugger does not. I've reported it as a bug.

Nobody is converting anything on input; it's merely a question of what character the editor inserts when you press the "return" key and your vagueness in the script. As I suggested above, this could be improved (in some sense) by displaying line breaks in strings using escape sequences. However, this means that if you typed this:

set x to "some
text"

...it would compile to this:

set x to "some\ntext" -- or possibly "some\rtext", depending on your editor.

If people are not disturbed by that, then it's a simple change to make -- you'd at least then be able to see what you were sending. However, I know that lots of people use lots of multi-line string constants, and they'd now all display on a single line, with an escape sequence to boot, which I know some people regard as inherently unreadable.


--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: Q: echo | sed -- Solved!
      • From: Nigel Smith <email@hidden>
References: 
 >Re: Q: echo | sed -- Solved! (From: Gnarlodious <email@hidden>)

  • Prev by Date: Re: First Applescript
  • Next by Date: Re: Finding the oldest date in a list
  • Previous by thread: Re: Q: echo | sed -- Solved!
  • Next by thread: Re: Q: echo | sed -- Solved!
  • Index(es):
    • Date
    • Thread