MACSCRPT Archives

May 2010

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
"Mark J. Reed" <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Tue, 18 May 2010 19:44:27 -0400
Content-Type:
text/plain
Parts/Attachments:
text/plain (57 lines)
On Tue, May 18, 2010 at 11:05 AM, jeff donovan <[log in to unmask]> wrote:
> so the right side {replace} is literal ?
>
> so should be able to sed 's/{myregex}/literal/g'
> no regex commands in the replace?

Right.  A regex is a pattern to match; it doesn't make sense to say
"replace this string with a pattern", since there are usually lots of
strings that will match a given pattern, frequently an infinite
number, and the software has no way of knowing which one you want to
use in the replacement..

>> if you want a tab or a newline, you have to put a literal tab
>> or newline (the latter backslash-escaped) inside the argument string.
>
> for some reason I thought that's what \t represented, I have also seen the use of an ascii representation

No, you misunderstand.  In many contexts, '\t' does indeed *represent*
a tab, but it is still not a *literal* tab character; it's a backslash
character followed by a t character.  What sed wants to see is the
actual tab character.  So your job is to figure out how to send that
character its way, either by typing it or by using the output of some
other command that generates it, like echo or tr.

> okay so ,.. like my next endeavor
> i want to change this;
> /usr/bin/perl -pi -e 's/\r\n?/\n/g' dosfile

The ? quantifier (0 or 1) is another one that doesn't work in OS X sed
without -E.  Quantifierwise, all you get without -E is *.  With -E,
your command should work as written:

sed -E -e "$(echo -e 's/\r\n?/\n/g')" dosfile >mytext

You can also leave off the -e on the sed command most of the time -
you only need it if you have more than one expression, or if the
expression starts with something that looks like an option.

sed -E "$(echo -e 's/\r\n?/\n/g')" dosfile >mytext

> Im trying to consolidate a script, this perl has been fine. I still may go back to it. But i would like to try other
> ways. some may be quicker. some take less resources.

Agreed, perl takes more resources than sed or awk, but it's easy to
get it to do what those programs do, and perl has the advantage of
being consistent across platforms.  I mostly still use sed and awk
only where the command line is shorter that way...e.g.  "awk '{print
$1}'" is only 16 characters, vs 24 for "perl -lane 'print $F[0]'". :)

Still, the inplace option makes Perl much nicer for a lot of these
sorts of operations (and is another feature that has been borrowed
from Perl by modern versions of GNU sed).


-- 
Mark J. Reed <[log in to unmask]>

ATOM RSS1 RSS2