Re: Using 'sed' to alter file
Re: Using 'sed' to alter file
- Subject: Re: Using 'sed' to alter file
- From: "Mark J. Reed" <email@hidden>
- Date: Thu, 6 Apr 2006 14:38:21 -0400
On 4/6/06,
Mark Walsh <
email@hidden> wrote:
I need to change the
characters that are between (but not including) "StringA" and "StringB" and
also all of the characters between (but not including) the first set of
parentheses after "StringC" (there are an unknown amount of characters
between "StringC" and the parentheses)
OK, there's a lot going on here. First of all, sed is a very line-oriented tool. If you're trying to do something complicated like this on text that isn't strictly delimited by lines, you're better off with something else, like Perl. This little Perl script should do the trick; if you save it as "
hyphenate.pl", then you can run it with "perl hyphenate.pl <oldfile >newfile", or as "perl -i file" to modify the file in place.
# Unset the delimiter so the read will slurp the whole file instead of just the first line
undef $/;
# read in the file
$_ = <>;
# split out the first part we want to change
my ($before, $changeme, $after) = /\A(.*StringA)(.*)(StringB.*)\Z/sm;
# change it
$changeme =~ s/[^a-zA-Z0-9:]/-/g;
# put it all back together
$_ = $before . $changeme . $after;
# split out the next part we want to change
($before, $changeme, $after) = /\A(.*StringC[^\(]*\()([^\)]*)(\).*)\Z/sm;
# change it
$changeme =~ s/[^a-zA-Z0-9:]/-/g;
# and put it all back together
$_ = $before . $changeme . $after;
# then print it back out
print;
--
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:
This email sent to email@hidden