我仍然在写这个小脚本,但是仍然有问题。获取错误
sed: no input files我认为问题出在:
for i in `cat sed_output.txt` do sed 's/$oldstr/$newstr/g' > sed_output_new_old_string.txt done
echo "Enter string to be replaced"
read OLDSTRING
echo "Enter string to be placed"
read NEWSTRING
oldstr=$OLDSTRING #string to be replaced
newstr=$NEWSTRING #new string to be placed
echo "Enter folder path where we will find the string"
read FOLDERPATH
### grep oldstring and output it in grep_output.txt
grep -rl $oldstr $FOLDERPATH > grep_output.txt
### spaces or special characters on filenames, use sed to enclose them with quote
for i in `cat grep_output.txt`
do sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/" grep_output.txt > sed_output.txt
done
for i in `cat sed_output.txt`
do sed 's/$oldstr/$newstr/g' > sed_output_new_old_string.txt
done发布于 2013-07-05 09:11:23
如果
了解需要对其执行substitution
然后;
sed 's/substitution/replacement/g' filename将逐行扫描文件以查找替换文本,并将其替换为替换文本。
对于递归替换,您可以这样做;
for file in path/to/folder; do
sed -i.bak 's/substitute/replacement/g' "$file"
done-i选项适用于文件更改。我给它添加了.bak,这样你就有了原始文件的备份,它将被重命名为file.bak。
如果您在sed中使用变量,那么我建议您使用"而不是',这样您的变量就可以正确地进行插值。
https://stackoverflow.com/questions/17479778
复制相似问题