Re: shell script replacing original file
- From: rthangam <ramesh.thangamani@xxxxxxxxx>
- Date: Thu, 27 Mar 2008 23:50:38 -0700 (PDT)
On Mar 28, 11:23 am, "Ming Ching TIew" <nos...@xxxxxxxxxx> wrote:
I want to know if it is 100 % safe to do this :-
$ cat myfile | sed -e '...........' > myfile
I have run this on my system, it seems to be doing
the job, but I need someone to confirm that it will
be 100 % safe.
The following commands wipe out the content
of the original file :-
$ sed -e ' .......' myfile > myfile
Of course I could do this also :-
$ sed -e '..........' myfile > /tmp/tmpfile
$ mv /tmp/tmpfile myfile
But that create uncessary intermediate files, and it has
the side effect of changing file permission and owner ship
of the original file.
Cheers.
Looks fine but using perl it is much simpler.
You can try using perl with options -p -i -e so that the changes will
be made inline no need to create a temporary copy also if you need to
take a back up of the existing files you can do it.
% cat test.txt
apple
orange
grapes
% perl -p -iold -e 's/apple/pineapple/g' test.txt
% cat test.txt
pineapple
orange
grapes
% ls test.*
test.txt test.txtold
What the command line options stand for:
-p assume loop like -n but print line also, like sed
-i[extension] edit <> files in place (makes backup if extension
supplied)
-e program one line of program (several -e's allowed, omit
programfile)
.
- Follow-Ups:
- Re: shell script replacing original file
- From: Ming Ching TIew
- Re: shell script replacing original file
- References:
- shell script replacing original file
- From: Ming Ching TIew
- shell script replacing original file
- Prev by Date: shell script replacing original file
- Next by Date: Re: shell script replacing original file
- Previous by thread: shell script replacing original file
- Next by thread: Re: shell script replacing original file
- Index(es):
Relevant Pages
|