【发布时间】:2020-12-21 16:15:54
【问题描述】:
特别是,我有一个包含指令列表:xxx/A.h、xxx/B.h、xxx/C.h...
我想在一个目录中的所有.h、.c 和.cpp 文件中用<xxx/X.h> 替换所有出现的"xxx/X.h"。
我在 shell 脚本中试过这个:
#!/bin/sh
name="xxx/A.h"
# This command successfully replaces "xxx/A.h" with <xxx/A.h> in A.cpp
sed -i "s;\"$name\";\<$name\>;g" A.cpp
# This command doesn't work. I need something like this to change the occurrences in all *.cpp, *.c files
find . -type f -a \( -name "*.h" -o -name "*.c" -o -name "*.hpp" -o -name "*.cpp" \) -a -exec sed -i "s;\"$name\";\<$name\>;g" {} +
为什么第二个命令不起作用?我该怎么做?
【问题讨论】:
-
怎么不行?如果您删除最后的
-exec,它会打印正确的文件吗?-a不是必须的,顺便说一下,它是隐含的。 -
请在您的问题中添加示例输入(无描述、无图像、无链接)以及该示例输入所需的输出(无评论)。
-
非常抱歉。事实证明,原因是
sed在处理一些没有权限的文件时停止,而那些预期要处理的文件没有被处理。这个命令确实有效。