Re: shell script replacing original file



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)
.



Relevant Pages

  • Re: Dumb printing question
    ... If you want to split the output and both drive the printer and save a file copy then look at the jetdirectprint script.. ... # cat jetdirectprint ... open a socket to the printer. ... My PERL is nonexistent, so don't ask me for syntactically correct PERL. ...
    (comp.os.linux.misc)
  • RE: sourcing one perl file from another
    ... There are some ways to 'source' a config file like under unix shell. ... $ cat usedata.pl ... I've a perl program, and I'm trying to make it tidier. ...
    (perl.beginners)
  • Re: question regarding inplace edit
    ... > Is it possible that if an inplace edit is attempted without creating a ... > backup file, perl cannot delete the original file to replace with the ...
    (perl.beginners)
  • Re: Barewood found where operator expected?
    ... So perl is telling you that you need to put an operator between /s/ and 'emile'. ... In the other two examples there are no quotes but there are also no whitespace or shell meta-characters in the perl code. ... If you use quotes then $(cat infra_east.txt) and $will not be iterpolated by the shell and perl will not understand it correctly. ...
    (perl.beginners)
  • Re: sed/awk, instead of Perl
    ... write it back to either a new file, the original file, or to STDOUT ... I'm curious why Perl isn't a decent choice. ... The perl command you wrote above is pretty much a sed ... Handelsregister: Registergericht Muenchen, HRA 74606, Geschäftsfuehrung: ...
    (freebsd-questions)