我想在几个文档中替换几个字符串.我知道如何sed使用该-e选项组合命令,但我不知道如何在脚本中将它们组合在一起运行多个文件.
我尝试使用'for'循环,但它不起作用.
for i in `ls *.txt`; do sed -e 's/22/twenty two/g' \ -e 's/23/twenty three/g'$i > new/$i; done
任何想法如何用做到这一点shell,awk,Python或Perl?
你在做:
for i in `ls *.txt`
do
   sed -e 's/22/twenty two/g' \ -e 's/23/twenty three/g'$i > new/$i
                              ^                       ^^^
                              why is this here?       missing space!
done
哪些可以改写为:
for i in *.txt  # <-- no need to `ls`!!
do
   sed -e 's/22/twenty two/g' -e 's/23/twenty three/g' "$i" > new/"$i"
done
所以你的问题是:
\在命令之间奇怪.sed 's...'文件名之间的空格.并提出一个建议:
ls,只需展开*.txt.小智 6
如果你使用awk,你不需要循环
awk '{gsub(/22/,"twenty two");gsub(/23/,"twenty three");print > "new/"FILENAME}' *.txt
| 归档时间: | 
 | 
| 查看次数: | 988 次 | 
| 最近记录: |