使用 ed 操作 find 匹配的文件

发布于 2024-09-01 04:56:16 字数 705 浏览 14 评论 0原文

遵循 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_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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南七夏 2024-09-08 04:56:16
find . -type f -exec bash -c 'ed -s "$2" <<< "$1"' _ 

POSIX 版本:

find . -type f -exec sh -c 'printf "%s\n" "g/a/s//b/g" w | ed -s "$1"' _ {} \;

使用 ed 而不是 sed -i 的主要原因是 ed 编辑文件,而 sed -i 覆盖文件,如果您碰巧有不想破坏的链接,那就是一个问题。

g/a/s//b/g\nw' {} \;

POSIX 版本:

使用 ed 而不是 sed -i 的主要原因是 ed 编辑文件,而 sed -i 覆盖文件,如果您碰巧有不想破坏的链接,那就是一个问题。

find . -type f -exec bash -c 'ed -s "$2" <<< "$1"' _ 

And a POSIX version:

find . -type f -exec sh -c 'printf "%s\n" "g/a/s//b/g" w | ed -s "$1"' _ {} \;

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.

g/a/s//b/g\nw' {} \;

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.

一瞬间的火花 2024-09-08 04:56:16

这个发现& 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

谁人与我共长歌 2024-09-08 04:56:16

将带有所有重定向和“此处文档”的 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文