我需要替换一个文件夹中的许多文件中的字符串,只有ssh访问服务器。我该怎么做呢?


当前回答

为了维护我个人的英文节点,我写了一个实用程序脚本,帮助替换多对旧/新字符串,递归为一个目录下的所有文件。

新/旧字符串的多个对在一个散列映射中进行管理。

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

其他回答

@kev的答案很好,但只影响直接目录中的文件。下面的示例使用grep递归地查找文件。这对我来说每次都很有效。

grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @

命令分解

Grep -r:——recursive,递归地读取每个目录下的所有文件。 Grep -l:——print-with-matches,打印每个有匹配项的文件的名称,而不是打印匹配行。 Grep -i:——忽略大小写。

xargs:将STDIN转换为参数,遵循这个答案。 Xargs -i@ ~命令包含@~:一个占位符,用于~命令中特定位置的参数,@符号是一个占位符,可以被任何字符串替换。

Sed -i:就地编辑文件,不进行备份。 Sed /regexp/replace /:替换匹配regexp的字符串。 Sed s/regexp/replacement/g: global,对每个匹配进行替换,而不是仅对第一个匹配进行替换。

如果文件包含反斜杠(通常是路径),你可以尝试这样做:

sed -i -- 's,<path1>,<path2>,g' *

ex:

sed -i -- 's,/foo/bar,/new/foo/bar,g' *.sh (in all shell scripts available)

我对许多答案的问题是,我需要替换许多文件中的文件路径。虽然有一个答案提到了这一点,但对我来说并不管用。我的解决方案:

首先,生成一个要更改的文件名列表。

filelist=($(find /path/to/your/folder | xargs grep '/path/to/fix' | cut -d : -f 1 | tr '\n' ' '))

上面的命令所做的是,管道到grep的find生成包含/路径/到/fix的文件名。然而,grep也打印出字符串所在的行,所以cut命令去掉了这个,只保留文件名。Tr将换行符替换为空格,允许将文件列表存储为数组。

for file in "${filelist[@]}"; do sed -i.bak 's+/path/to/fix+/new/path/for/my/file+g' $file; done

这个sed命令利用了这个问题的其他答案,并使用+作为分隔符而不是普通的/,因为文件路径中使用了/字符。

如果你有可以使用的文件列表

replace "old_string" "new_string" -- file_name1 file_name2 file_name3

如果你有所有可以使用的文件

replace "old_string" "new_string" -- *

如果你有文件扩展名列表,你可以使用

replace "old_string" "new_string" -- *.extension

我从另一篇文章中找到了这篇文章(不记得是哪篇了),虽然不是最优雅的,但它很简单,作为一个新手Linux用户,它没有给我带来任何麻烦

for i in *old_str* ; do mv -v "$i" "${i/\old_str/new_str}" ; done

如果有空格或其他特殊字符,请使用\

for i in *old_str\ * ; do mv -v "$i" "${i/\old_str\ /new_str}" ; done

对于子目录中的字符串使用**

for i in *\*old_str\ * ; do mv -v "$i" "${i/\old_str\ /new_str}" ; done