Sed 无法编辑:不是常规文件

T-R*_*Rex 6 shell sed cygwin find

我已经尝试了 SO 中提到的各种解决方案来解决我面临的这个问题。

我想递归地查找所有文件并更改某些文本。

这是我在 Windows 中的 cygwin 中使用的命令

 find . -not -path \*.hg\* -exec sed -i 's/FCLP1025/FCLP1080/g' {} \;
Run Code Online (Sandbox Code Playgroud)

我得到的结果是

sed: couldn't edit ./xxx/yyy/filename: not a regular file
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么吗?

leo*_*eon 6

find返回所有文件和所有目录的列表。您应该限制搜索范围,尝试查找实际包含该字符串的文件。您可以尝试添加:

find -type f ... #  Doesn't return directories
find -iname "*.txt" ... #  returns only files ending in '.txt' (case insensitive)
Run Code Online (Sandbox Code Playgroud)

或者,如果您真的想变得更奇特,您可以尝试使用 grep 仅在实际包含该字符串的文件上运行 sed:

grep -l -r 'FCLP1025' | xargs sed -i 's/FCLP1025/FCLP1080/g'
Run Code Online (Sandbox Code Playgroud)

grep-r递归搜索,并-l仅列出匹配文件的文件名。xargs然后使用 stdin 将其作为参数添加到以下命令中,这与{}使用时类似find -exec cmd {} \;

不过,在 Linux 上的 bash 中进行了测试,因此如果它不起作用,您可能想grep --help在 git-bash/cygwin 中查看标志是否不同。但通常它们是相同的。