Re: What is the best way to change the content (string of text) of a system file?
Re: What is the best way to change the content (string of text) of a system file?
- Subject: Re: What is the best way to change the content (string of text) of a system file?
- From: "Mark J. Reed" <email@hidden>
- Date: Thu, 9 Mar 2006 12:28:15 -0500
On 3/9/06, Bernardo Höhl <email@hidden> wrote:
> I have used "echo" applescript's original "write to file", but I
> wonder if I could use an Unix command to find for a string of text
> and replace it, inside a system file.
In UNIXland there are a variety of ways of doing this, depending on
exactly what you're looking for and how you're deciding what to change
it to.
If you're looking for a unique string (that only appears once in the
file) and just replacing the entire thing with something else, the
traditional mechaism is to use sed into a new file and then copy the
new file in place of the original. The more modern mechanism is to
use perl -i, which does an in-place edit of the file, so no temporary
file or renaming is required. Thus:
sed -e 's/old string/new string/' filename >file.tmp; mv file.tmp filename
or
perl -pi -e 's/old string/new string/' filename
If you want to save a backup of the file as it existed before your
change - always a good idea - you can cp it first, or let Perl do it
for you by giving a suffix as an argument to the -i option:
perl -pi.bak -e 's/old string/new string/' filename
The above command will make a copy of 'filename' called 'filename.bak'
before applying the change to it.
--
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