LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to make alterations to a file using sed , saving changes and retaining filename ? (http://www.linuxquestions.org/questions/linux-general-1/how-to-make-alterations-to-a-file-using-sed-saving-changes-and-retaining-filename-536034/)

uncle-c 03-09-2007 11:37 AM

How to make alterations to a file using sed , saving changes and retaining filename ?
 
Something which has been gnawing at me. I want to run a sed operation on a file / list of files, save the changes whilst keeping the original file name.

eg

Code:

$ sed '*******'  file1 > file1

I'd assume this would overwrite the original file.
I've tried adjusting the noclobber settings, but I still get a file of size zero bytes.
Would the best bet be to append to say file2 , then delete the original ( file1 ) and rename file2 as file1 ?
Basically I have a directory full of html files and I need to delete specific lines but to retain the original file names as the files are hyperlinked with each other
Perhaps it could be done in Perl, but I already have the working sed syntax .
At present this sed problem is the rate limiting step in the overall shell script completion.

cheers
uncle.

ps : It is not homework !!! If only I was that young again ! :)

syg00 03-09-2007 11:52 AM

"man sed" - look particularly for -i

leftblank 07-02-2009 10:46 AM

Use the -i switch with an extension
 
-i is inline
-i.bak (No space) will make a .bak

sed -i.bak -e 's/findthis/changetothis/' /home/yourdir/yourtestfile


All times are GMT -5. The time now is 08:58 PM.