空运行中的sed命令 [英] sed command in dry run

查看:149
本文介绍了空运行中的sed命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆