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.
现在这似乎找到了字符串“this is a”并用“I am a”替换,但这只会打印到屏幕上。
I need it to replace in the files themselves.
我需要它来替换文件本身。
Thanks
谢谢
4 个解决方案
#1
27
Use the -i
option of sed
to make the changes in place:
使用sed的-i选项进行更改:
sed -i "s/$OLD/$NEW/g" "$f"
^^
#2
2
The output goes to screen (stdout
) because of the following:
输出进入屏幕(stdout),原因如下:
sed "s/$OLD/$NEW/g" "$f"
Try redirecting to a file (the following redirects to a new files and then renames it to overwrite the original file):
尝试重定向到一个文件(以下是重定向到一个新文件,然后重命名它以覆盖原始文件):
sed "s/$OLD/$NEW/g" "$f" > "$f.new" && mv "$f.new" "$f"
#3
0
this is a snippet i use, it removes all stuff between APA and BEPA (across multiple lines, including removing APA, BEPA) in all files below current directory, exclude the .svn directory
这是我使用的一个片段,它在当前目录下的所有文件中删除APA和BEPA(跨多行,包括删除APA, BEPA)之间的所有内容,排除.svn目录
find . \! -path '*.svn*' -type f -exec sed -i -n '1h;1!H;${;g;s/APA[ \t\r\n]*BEPA//g;p}' {} \;
#4
0
Check this out
看看这个
http://cs.boisestate.edu/~amit/teaching/handouts/cs-unix/node130.html
http://cs.boisestate.edu/ amit /教学/救济/ cs-unix / node130.html
##########################################################
\#!/bin/sh
\# sed/changeword
prog=`basename $0`
case $# in
0|1) echo 'Usage:' $prog '<old string> <new string>'; exit 1;;
esac
old=$1
new=$2
for f in *
do
if test "$f" != "$prog"
then
if test -f "$f"
then
sed "s/$old/$new/g" $f > $f.new
mv $f $f.orig
mv $f.new $f
echo $f done
fi
fi
done
##############################################################
#1
27
Use the -i
option of sed
to make the changes in place:
使用sed的-i选项进行更改:
sed -i "s/$OLD/$NEW/g" "$f"
^^
#2
2
The output goes to screen (stdout
) because of the following:
输出进入屏幕(stdout),原因如下:
sed "s/$OLD/$NEW/g" "$f"
Try redirecting to a file (the following redirects to a new files and then renames it to overwrite the original file):
尝试重定向到一个文件(以下是重定向到一个新文件,然后重命名它以覆盖原始文件):
sed "s/$OLD/$NEW/g" "$f" > "$f.new" && mv "$f.new" "$f"
#3
0
this is a snippet i use, it removes all stuff between APA and BEPA (across multiple lines, including removing APA, BEPA) in all files below current directory, exclude the .svn directory
这是我使用的一个片段,它在当前目录下的所有文件中删除APA和BEPA(跨多行,包括删除APA, BEPA)之间的所有内容,排除.svn目录
find . \! -path '*.svn*' -type f -exec sed -i -n '1h;1!H;${;g;s/APA[ \t\r\n]*BEPA//g;p}' {} \;
#4
0
Check this out
看看这个
http://cs.boisestate.edu/~amit/teaching/handouts/cs-unix/node130.html
http://cs.boisestate.edu/ amit /教学/救济/ cs-unix / node130.html
##########################################################
\#!/bin/sh
\# sed/changeword
prog=`basename $0`
case $# in
0|1) echo 'Usage:' $prog '<old string> <new string>'; exit 1;;
esac
old=$1
new=$2
for f in *
do
if test "$f" != "$prog"
then
if test -f "$f"
then
sed "s/$old/$new/g" $f > $f.new
mv $f $f.orig
mv $f.new $f
echo $f done
fi
fi
done
##############################################################