|1 2 3 4 5 6 7 8 9 10 11 12|
There is no guarantee that your questions here will ever be answered. You can be published anonymously - just let us know!
From Peagler Kelley
Answered By Ben Okopnik
I am doing a global search and replace on some files via a unix script. I use the sed command (saving a copy of the original in $file.old) like so:
sed "s/$INPUT_TXT/$OUTPUT_TXT/g" $file > $NEW_FILE
Then I perform a diff on the original file ($file) and the new file ($NEW_FILE) and redirect the output to /dev/null. If there is a difference between the two files, then I move the new file to the old file. Unfortunately I end up changing the permissions on all the files in the directory, depending on whatever default umask is set. Is there a way that I can either 1) find out what the permissions of the original file are and change them accordingly to the new file, or 2) move the new file to the original file while keeping the permissions of the old file?? Please let me know. Thanks!!
[Ben] "sed" is not the best tool for "in-place editing" of the kind you want to do - all you really want is to change the data in the file, right? Perl offers a solution that reduces it from a three-step process (change/diff/move) to one:
perl -i -wpe 's/$INPUT_TXT/$OUTPUT_TXT/g' $file
That's it. The editing is done in the file itself; the permissions are unchanged. No muss, no fuss, no greasy aftertaste.
I agree with you. I wanted to use perl, but the person who I'm creating this for does not know perl and will be responsible for supporting it. I like your quick, dirty solution and I may force them to use it just because it's easier . Thanks!
|1 2 3 4 5 6 7 8 9 1 1 1|