使用 ed 操作 find 匹配的文件
遵循 bash-hackers wiki 的建议,我想要使用 ed 编辑文件。
特别是我想用 ed 而不是 sed 执行以下操作:
find . -type f -exec sed -i -e 's/a/b/g' {} \;
我发现 ed 没有像 sed 的 -e 这样的 opt ,所以据我所知,管道和 io 重定向是非交互式使用它的唯一方法。
因此,使用 bash 脚本中的 ed 执行与上述 sed 命令相同的操作将如下所示:
ed file_name <<<$'g/a/s//b/g\nw'
或者
echo $'g/a/s//b/g\nw' | ed file_name
但据我所知,在 find 的 -exec 中不可能涉及管道或 io 重定向。
我错过了什么吗?或者克服这个问题的唯一方法是使用循环?
for file in $(find . -type f -print); do ed $file <<<$'g/a/s//b/g\nw'; done;
Following the bash-hackers wiki's recommendation, I want to edit files using ed.
In particular I want to do the following with ed instead of sed:
find . -type f -exec sed -i -e 's/a/b/g' {} \;
I see that ed doesn't have opt like sed's -e, so as far as I know, pipes and io redirections are the only way to work with it non-interactively.
So, using ed from a bash script to do the same as the above sed command would look like:
ed file_name <<<
Or
echo
But as far as I know it is impossible to involve pipes or io redirections within find's -exec.
Do I miss something? or is the only way to overcome this is to use loops?
for file in $(find . -type f -print); do ed $file <<<
g/a/s//b/g\nw'
Or
But as far as I know it is impossible to involve pipes or io redirections within find's -exec.
Do I miss something? or is the only way to overcome this is to use loops?
g/a/s//b/g\nw' | ed file_name
But as far as I know it is impossible to involve pipes or io redirections within find's -exec.
Do I miss something? or is the only way to overcome this is to use loops?
g/a/s//b/g\nw'
Or
But as far as I know it is impossible to involve pipes or io redirections within find's -exec.
Do I miss something? or is the only way to overcome this is to use loops?
g/a/s//b/g\nw'; done;g/a/s//b/g\nw'Or
But as far as I know it is impossible to involve pipes or io redirections within find's -exec.
Do I miss something? or is the only way to overcome this is to use loops?
g/a/s//b/g\nw' | ed file_nameBut as far as I know it is impossible to involve pipes or io redirections within find's -exec.
Do I miss something? or is the only way to overcome this is to use loops?
g/a/s//b/g\nw'Or
But as far as I know it is impossible to involve pipes or io redirections within find's -exec.
Do I miss something? or is the only way to overcome this is to use loops?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
POSIX 版本:
使用 ed 而不是 sed -i 的主要原因是 ed 编辑文件,而 sed -i 覆盖文件,如果您碰巧有不想破坏的链接,那就是一个问题。
And a POSIX version:
The main reason to use ed over sed -i is that ed edits files while sed -i overwrites files, and that's a problem if you happen to have links you don't want to break.
这个发现& ed 片段具有测试模式(因此您可以在实际批量替换操作之前验证您的正则表达式)
:替换为查找 &编
http://codesnippets.joyent.com/posts/show/2299
This find & ed snippet features a test mode (so you can verify you got your regex right before an acutal mass replace operation):
Search & replace with find & ed
http://codesnippets.joyent.com/posts/show/2299
将带有所有重定向和“此处文档”的 ed 命令放入 shell 脚本中,然后从 find 中调用它。
但我不得不问:为什么是ed?您认为 ed 能做什么而 sed 不能做什么?
Put the ed command with all the redirections and "here documents" in a shell script, and invoke that from find.
But I have to ask: why ed? What do you think you can do with ed that you can't do with sed?