Re: Search and replace with Unix commands
Re: Search and replace with Unix commands
- Subject: Re: Search and replace with Unix commands
- From: "Mark J. Reed" <email@hidden>
- Date: Wed, 4 Aug 2010 20:43:45 -0400
Perl will happily deal with files as single chunks instead of just a
line at a time; it's just a bit more work to specify. This should do
the trick:
set text item delimiters to linefeed
set oldText to { "A line", "another line", "a third line", "", "SOME
STRING" } as text
set newText to paragraphs of (do shell script "perl -0777 -pe
's/^\\nSOME STRING/REPLACEMENT/gms' <<<" & (quoted form of oldText))
as text
Some explanations follow, if you're curious about the details of the Perl.
The -0777 option tells Perl to read the entire file as one big string.
Specifically, the syntax is -0 + the octal value of a single-byte
character which should be taken as the record delimiter; since 777 is
more than a byte, no character matches and the whole file is one
record. Hacky but effective. (There's a different syntax for
specifying an arbitrary Unicode character as the delimiter.)
The -p option tells perl to loop over its input, one iteration per
record as defined by the -0 option, and run the supplied code as the
body of the loop, with the "topic" variable $_ set to the input
record.
The -e option, followed by some Perl code, tells Perl to execute that
code (instead of reading its code from standard input or a file).
s/^\nSOME STRING/NEW TEXT/gms
replaces an empty line followed by "SOME STRING" with "NEW TEXT". If
you want to extend your definition of "empty line" to include lines
that contain whitespace but nothing else, use this instead:
s/^\s*\nSOME STRING/NEW TEXT/gms
The /gms are three options to the pattern match:
/g says to find (and replace) all occurrences, not just the first one.
/m changes the meaning of '^' to match at the start of any
embedded line, not just at the start of the whole string.
/s makes the "match any character" character '.' match newlines,
which it normally doesn't. Not needed here since there are no '.'s in
the pattern, but it's a good habit to put /s on there any time you're
matching a string that contains embedded newlines.
On Wed, Aug 4, 2010 at 8:08 PM, Hagimeno <email@hidden> wrote:
> Hi,
>
> I need to search a string like '\rJohn' or '\nJohn' with another string
> All the unix command (Sed, Awk...) works one line at time and the separator is usually LF.
> How can I search and replace, not using pure AS because some text file are really bigger?
> I would like to use do shell script using cat to read the file and some Unix command to search empty line followed by my string and replace them with something else.
>
> Mark
>
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> AppleScript-Users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden
>
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden