I'm trying to find and replace a string in a folder of files.
Could someone possibly help me?
My script is as follows:
#!/bin/bash
OLD="This is a"
NEW="I am a"
DPATH="/home/user/test/*.txt"
BPATH="/home/user/test/backup/foo"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
if [ -f $f -a -r $f ]; then
/bin/cp -f $f $BPATH
sed "s/$OLD/$NEW/g" "$f"
else
echo "Error: Cannot read $f"
fi
done
Now this seems to find the string 'This is a' and replaces with 'I am a', but this only prints to screen.
I need it to replace in the files themselves.
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…