LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   saving files after sed processing (https://www.linuxquestions.org/questions/linux-general-1/saving-files-after-sed-processing-26351/)

raypen 07-23-2002 12:15 AM

saving files after sed processing
 
SED doesn't seem to have a way to save a file once
it has been processed by sed commands. The only
examples I have seen involve piping the output of
sed processing to a temporary file, deleting the old
file and renaming the temporary file, giving it the
same name as the old.

Is there a way to perform file processing with sed
and saving the changed file without going through
the aforementioned intermediate steps?

Can PERL be used to perform changes w/o the
intermediate steps? Which is best?

neo77777 07-23-2002 12:43 AM

Yes perl has an ability of inplace editing if you need class notes for my old perl class, let me see if I can submit it here w/o violating laws.

Mik 07-23-2002 06:38 AM

Well the perl inplace editing is just a wrapper. In the background it will still do something similar to what is done with sed and then move. Anyway to do the inplace editing you can make the following script:
Code:

#!/usr/bin/perl -i

$search = "something";
$replace = "else";

while ( <> ) {
    s/$search/$replace/;
    print $_;
}

Just call the script with 'scriptname filetomodify'.

To do the same with sed is not that much harder it would just be one line. Something like:

sed -e 's/something/else/' file > file.bak && mv -f file.bak file

It would probably depend on what you are planning to use it for, which one of the two might be easier.


All times are GMT -5. The time now is 03:42 PM.