我需要替换一个文件夹中的许多文件中的字符串,只有ssh访问服务器。我该怎么做呢?
当前回答
如果文件包含反斜杠(通常是路径),你可以尝试这样做:
sed -i -- 's,<path1>,<path2>,g' *
ex:
sed -i -- 's,/foo/bar,/new/foo/bar,g' *.sh (in all shell scripts available)
其他回答
有一个更简单的方法,使用一个简单的脚本文件:
# sudo chmod +x /bin/replace_string_files_present_dir
在gedit或你选择的编辑器中打开文件,我在这里使用gedit。
# sudo gedit /bin/replace_string_files_present_dir
然后在编辑器中将以下内容粘贴到文件中
#!/bin/bash
replace "oldstring" "newstring" -- *
replace "oldstring1" "newstring2" -- *
#add as many lines of replace as there are your strings to be replaced for
#example here i have two sets of strings to replace which are oldstring and
#oldstring1 so I use two replace lines.
保存文件,关闭gedit,然后退出您的终端,或者只是关闭它,然后启动它,以便能够加载您添加的新脚本。
导航到有多个要编辑的文件的目录。然后运行:
#replace_string_files_present_dir
按enter键,这将自动将包含它们的所有文件中的oldstring和oldstring1分别替换为正确的newstring和newstring1。
它将跳过不包含旧字符串的所有目录和文件。
如果您有多个目录的文件需要替换字符串,这可能有助于消除乏味的输入工作。你所要做的就是导航到这些目录,然后运行:
# replace_string_files_present_dir
你所要做的就是确保你已经包括或添加了所有替换字符串,就像我上面展示的那样:
替换 “oldstring” “newstring” -- *
在文件/bin/replace_string_files_present_dir的末尾。
要添加一个新的替换字符串,只需打开我们创建的脚本,在终端中输入以下命令:
Sudo gedit /bin/replace_string_files_present_dir
不要担心你添加的替换字符串的数量,如果没有找到oldstring,它们将没有任何影响。
使用ack命令会快得多,像这样:
ack '25 Essex' -l | xargs sed -i 's/The\ fox \jump/abc 321/g'
如果你在搜索结果中有空白。你需要逃离它。
在我发现这个问题(和答案)之前,我确实编造了自己的解决方案。 我搜索了“replace”、“few”和“xml”的不同组合,因为那是我的应用程序,但没有找到这个特定的组合。
我的问题:我有spring xml文件,其中包含测试用例的数据,其中包含复杂的对象。java源代码上的重构改变了许多类,但不适用于xml数据文件。为了保存测试用例数据,我需要更改所有xml文件中的所有类名,这些文件分布在几个目录中。同时保存原始xml文件的备份副本(尽管这不是必须的,因为版本控制可以在这里保存我)。
我正在寻找find + sed的某种组合,因为它在其他情况下也适用,但不能同时使用几个替换。
然后我找到了ask ubuntu response,它帮助我构建了我的命令行:
find -name "*.xml" -exec sed -s --in-place=.bak -e 's/firstWord/newFirstWord/g;s/secondWord/newSecondWord/g;s/thirdWord/newThirdWord/g' {} \;
而且效果非常好(好吧,我的箱子有六种不同的替代品)。但请注意,它将触及当前目录下的所有*.xml文件。正因为如此,如果你要对版本控制系统负责,你可能想要先进行过滤,只将那些实际拥有你想要的字符串传递给sed;如:
find -name "*.xml" -exec grep -e "firstWord" -e "secondWord" -e "thirdWord" {} \; -exec sed -s --in-place=.bak -e 's/firstWord/newFirstWord/g;s/secondWord/newSecondWord/g;s/thirdWord/newThirdWord/g' {} \;
当使用-i开关调用时,流编辑器确实会“inplace”修改多个文件,该开关以备份文件结尾作为参数。所以
sed -i.bak 's/foo/bar/g' *
在此文件夹中的所有文件中,将foo替换为bar,但不会下降到子文件夹中。但是,这将为目录中的每个文件生成一个新的.bak文件。 要递归地为该目录及其所有子目录中的所有文件执行此操作,您需要一个助手(如find)来遍历目录树。
find ./ -print0 | xargs -0 sed -i.bak 's/foo/bar/g' *
Find允许您进一步限制要修改的文件,通过指定进一步的参数,如Find ./ -name '*.php' -或name '*.html' -print0(如有必要)。
注意:GNU sed不需要文件结尾,sed -i 's/foo/bar/g' *也可以;FreeBSD sed要求扩展,但允许中间有一个空格,因此sed -i .bak s/foo/bar/g *可以工作。
multiedit命令脚本
multiedit [-n PATTERN] OLDSTRING NEWSTRING
根据Kaspar的回答,我编写了一个bash脚本来接受命令行参数,并有选择地限制与模式匹配的文件名。保存在$PATH中并使其可执行,然后使用上面的命令。
剧本如下:
#!/bin/bash
_help="\n
Replace OLDSTRING with NEWSTRING recursively starting from current directory\n
multiedit [-n PATTERN] OLDSTRING NEWSTRING\n
[-n PATTERN] option limits to filenames matching PATTERN\n
Note: backslash escape special characters\n
Note: enclose STRINGS with spaces in double quotes\n
Example to limit the edit to python files:\n
multiedit -n \*.py \"OLD STRING\" NEWSTRING\n"
# ensure correct number of arguments, otherwise display help...
if [ $# -lt 2 ] || [ $# -gt 4 ]; then echo -e $_help ; exit ; fi
if [ $1 == "-n" ]; then # if -n option is given:
# replace OLDSTRING with NEWSTRING recursively in files matching PATTERN
find ./ -type f -name "$2" -exec sed -i "s/$3/$4/g" {} \;
else
# replace OLDSTRING with NEWSTRING recursively in all files
find ./ -type f -exec sed -i "s/$1/$2/" {} \;
fi
推荐文章
- 如何找到Java堆大小和内存使用(Linux)?
- 遍历带空格的文件列表
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- Bash:无限睡眠(无限阻塞)
- 如何使Python脚本在Linux中像服务或守护进程一样运行
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中解析字符串为日期时间
- 如何使用文件的行作为命令的参数?
- 字符串中的单词大写
- time_t最终的类型定义是什么?
- string.ToLower()和string.ToLowerInvariant()
- PHP字符串中的花括号
- 如何删除字符串的第一个和最后一个字符
- 如何使用cut为分隔符指定更多的空格?