How to replace text in text file using .SH file script?

So I want to create a script that takes 3 arguments - path to file, exact word to replace and with what to replace it. How to create such thing?

Generally I want6 it to have api like sudo script.sh "C:/myTextDoc.xml" "_WORD_TO_REPLACE_" "WordTo Use"


你不需要一个脚本,一个简单的sed就可以做到(如果你使用cygwin或POSIX兼容的操作系统):

sed -i '' 's/_WORD_TO_REPLACE_/WordTo Use/' "C:/myTextDoc.xml"

Something like this?

#!/bin/bash
sed -e "s/$2/$3/g" <$1 >$1.$$ && cp $1.$$ $1 && rm $1.$$

Alternatively, you can use the single command

sed -i -e "s/$2/$3/g" $1

as Yan suggested. I generally use the first form myself. I have seen systems where -i is not supported (SunOS).

This will replace all instances of the second argument with the third, in the file passed as the first. For example, ./replace file oldword newword


红宝石(1.9+)

$ ruby -i.bak -ne 'print $_.gsub(/WORD_TO_REPLACE/,"New Word")' /path/to/file
链接地址: http://www.djcxy.com/p/35428.html

上一篇: 使用命令行将Flex CSS文件编译成SWF文件

下一篇: 如何使用.SH文件脚本替换文本文件中的文本?