【问题标题】:Replace some strings in files with specific extensions using `sed`使用`sed`替换具有特定扩展名的文件中的一些字符串
【发布时间】:2020-12-21 16:15:54
【问题描述】:

特别是,我有一个包含指令列表:xxx/A.hxxx/B.hxxx/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 在处理一些没有权限的文件时停止,而那些预期要处理的文件没有被处理。这个命令确实有效。

标签: linux sed sh


【解决方案1】:

原来原因是sed在处理一些没有权限的文件时停止了,而那些预期被处理的文件没有被处理。

【讨论】:

    猜你喜欢
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2020-02-15
    • 2021-07-04
    相关资源
    最近更新 更多