[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Replacing text in multiple files
From: |
Paul Jarc |
Subject: |
Re: Replacing text in multiple files |
Date: |
Mon, 12 Nov 2001 16:08:09 -0500 |
User-agent: |
Gnus/5.090004 (Oort Gnus v0.04) Emacs/20.7 (i386-redhat-linux-gnu) |
"Jaya Reddy" <address@hidden> wrote:
> gawk '{gsub("oldstr","newstr"); print $0}' file1 > file1
In this command, the shell opens and truncates file1 for output before
gawk ever sees it. Use this instead:
gawk '{gsub("oldstr","newstr"); print $0}' file1 > file2
mv file2 file1
This has the added advantage that file1 is updated atomically by mv;
anything that looks at file1 at any time will see either the complete
old version or the complete new version - never a half-written new
version.
> find /home/gnuuser/mydir -type f -exec gawk '{gsub("oldstr","newstr");
> print $0 > FILENAME }' {} \;
This should work too:
find /home/gnuuser/mydir -type f -exec \
sh -c 'gawk "$0" "$1" > "$1.tmp"; mv "$1.tmp" "$1"' \
'{gsub("oldstr","newstr"); print $0}' '{}' \;
Here, the redirection is done by the shell invoked by find.
paul