空运行中的sed命令 [英] sed command in dry run
问题描述
如何用 sed
进行空运行?
我有以下命令:
I have this command:
find ./ -type f | xargs sed -i 's/string1/string2/g'
但是在我真正替代所有文件,我想检查一下它将替代什么。复制整个目录结构以进行检查是不可能的!
But before I really substitute in all the files, i want to check what it WOULD substitute. Copying the whole directory structure to check is no option!
推荐答案
删除 -i
并将其通过管道传递到 less
进行分页。或者,您可以通过删除 -i
并附加>来将整个内容重定向到一个大文件。 dryrun.out
Remove the -i
and pipe it to less
to paginate though the results. Alternatively, you can redirect the whole thing to one large file by removing the -i
and appending > dryrun.out
我应该注意,对于您的脚本,如果名称中包含空格或其他有害字符(例如换行符)的文件,将严重失败还是什么。更好的方法是:
I should note that this script of yours will fail miserably with files that contain spaces in their name or other nefarious characters like newlines or whatnot. A better way to do it would be:
while IFS= read -r -d $'\0' file; do
sed -i 's/string1/string2/g' "$file"
done < <(find ./ -type f -print0)
这篇关于空运行中的sed命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!