2010-05-10 54 views
3

bash-hackers wiki's recommendation之后,我想使用ed编辑文件。使用ed来操纵查找匹配的文件

我特别想做的ED以下而不使用sed:

find . -type f -exec sed -i -e 's/a/b/g' {} \; 

我看到ED并没有选择像sed的-e,从而据我所知,管道和IO重定向是以非交互方式使用它的唯一方法。

因此,使用编辑从bash脚本做同上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; 

回答

4
find . -type f -exec bash -c 'ed -s "$2" <<< "$1"' _ $'g/a/s//b/g\nw' {} \; 

而且一个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覆盖文件,如果你碰巧有这是一个问题你不想破坏的链接。

0

将带有所有重定向和“here文件”的ed命令放在shell脚本中,并从查找中调用该命令。我不得不问:为什么要编辑?你认为你可以用ed做什么,你无法使用sed?

+0

查看我刚才提到的链接,以及geirha的回答。 – theosp 2010-05-11 00:01:06