[ Table Of Contents ][ Answer Guy Current Index ] greetings   1   2   3   4   5   6   7   8   9   10   11   12 [ Index of Past Answers ]


(?) The Answer Gang (!)


By Jim Dennis, Ben Okopnik, Dan Wilder, Breen, Chris, and the Gang, the Editors of Linux Gazette... and You!
Send questions (or interesting answers) to

There is no guarantee that your questions here will ever be answered. You can be published anonymously - just let us know!


(?) Search and Replace Without Breaking Permissions

From Peagler Kelley

Answered By Ben Okopnik

Hi,

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!


This page edited and maintained by the Editors of Linux Gazette Copyright © 2001
Published in issue 66 of Linux Gazette May 2001
HTML script maintained by of Starshine Technical Services, http://www.starshine.org/


[ Table Of Contents ][ Answer Guy Current Index ] greetings   1   2   3   4   5   6   7   8   9   1   1   1 [ Index of Past Answers ]