我需要替换一个文件夹中的许多文件中的字符串,只有ssh访问服务器。我该怎么做呢?
当前回答
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
其他回答
真的很蹩脚,但我不能让任何sed命令在OSX上工作,所以我做了这个愚蠢的事情:
:%s/foo/bar/g
:wn
^-复制这三行到我的剪贴板(是的,包括结束换行),然后:
vi *
按住command-v,直到它显示没有文件了。
愚蠢的…出租汽车司机…有效…
对于这个问题,已经列出了一些标准答案。通常,您可以使用find来递归地列出文件,然后使用sed或perl执行操作。
rpl
对于大多数快速使用,您可能会发现rpl命令更容易记住。
在所有。txt文件上用bar替换foo:
rpl -v foo bar '*.txt'
模拟替换正则表达式foo。*在所有.txt文件中递归地使用bar:
rpl --dry-run 'foo.*' bar '**/*.txt'
您可能需要安装它(apt-get install rpl或类似的方法)。
repren
然而,对于涉及正则表达式和反向替换,或文件重命名以及搜索和替换的更困难的工作,我所知道的最通用和最强大的工具是repren,这是我不久前为一些棘手的重命名和重构任务编写的一个小Python脚本。你可能更喜欢它的原因是:
支持重命名文件以及搜索和替换文件内容。 在执行搜索和替换之前查看更改。 支持带有反向替换、整词、大小写不敏感和保留大小写(replace foo -> bar, foo -> bar, foo -> bar)模式的正则表达式。 适用于多个替换,包括互换(foo -> bar和bar -> foo)或非唯一替换集(foo -> bar, f -> x)。
使用它,pip安装repren。查看README中的示例。
grep --include={*.php,*.html} -rnl './' -e "old" | xargs -i@ sed -i 's/old/new/g' @
使用ack命令会快得多,像这样:
ack '25 Essex' -l | xargs sed -i 's/The\ fox \jump/abc 321/g'
如果你在搜索结果中有空白。你需要逃离它。
为了维护我个人的英文节点,我写了一个实用程序脚本,帮助替换多对旧/新字符串,递归为一个目录下的所有文件。
新/旧字符串的多个对在一个散列映射中进行管理。
dir可以通过命令行或环境变量设置,映射是硬编码在脚本中,但如果需要,您可以修改代码从文件加载。
由于一些新特性,它需要bash 4.2。
en_standardize.sh:
#! /bin/bash
# (need bash 4.2+,)
#
# Standardize phonetic symbol of English.
#
# format:
# en_standardize.sh [<dir>]
#
# params:
# * dir
# target dir, optional,
# if not specified then use environment variable "$node_dir_en",
# if both not provided, then will not execute,
# *
#
paramCount=$#
# figure target dir,
if [ $paramCount -ge 1 ]; then # dir specified
echo -e "dir specified (in command):\n\t$1\n"
targetDir=$1
elif [[ -v node_dir_en ]]; then # environable set,
echo -e "dir specified (in environment vairable):\n\t$node_dir_en\n"
targetDir=$node_dir_en
else # environable not set,
echo "dir not specified, won't execute"
exit
fi
# check whether dir exists,
if [ -d $targetDir ]; then
cd $targetDir
else
echo -e "invalid dir location:\n\t$targetDir\n"
exit
fi
# initial map,
declare -A itemMap
itemMap=( ["ɪ"]="i" ["ː"]=":" ["ɜ"]="ə" ["ɒ"]="ɔ" ["ʊ"]="u" ["ɛ"]="e")
# print item maps,
echo 'maps:'
for key in "${!itemMap[@]}"; do
echo -e "\t$key\t->\t${itemMap[$key]}"
done
echo -e '\n'
# do replace,
for key in "${!itemMap[@]}"; do
grep -rli "$key" * | xargs -i@ sed -i "s/$key/${itemMap[$key]}/g" @
done
echo -e "\nDone."
exit