【问题标题】:Commenting out lines with a specific substring across multiple files in Sublime/Linux在 Sublime/Linux 中跨多个文件用特定子字符串注释行
【发布时间】:2021-04-17 00:42:24
【问题描述】:

我有一堆文件,其中包含我想要注释掉的包含特定字符串的行。

在下面的示例中,我只需要注释掉带有子字符串Sensor 的行。我正在使用 Sublime 编辑器,所以这样做会很棒,但是如果有一些 linux 命令的可能性,那也可以。

//fileA.c
printf ("Sensor reading A");  // need to comment out this
// ...

// fileB.C
printf ("Function starts...");
printf ("Sensor A value");   // need to comment out this
// ...

【问题讨论】:

    标签: linux ide sublimetext3 sublimetext


    【解决方案1】:

    在外壳中:

    for file in *.c; do # Adjust as needed
        printf '%s\n' 'g/Sensor/s!^!//!' w | ed -s "$file"
    done
    

    对于每个文件,使用标准 POSIX 文件编辑器ed 打开它,并将// 添加到包含Sensor 的任何行的开头,然后保存文件。

    【讨论】:

    • 谢谢。介意在这里解释正则表达式吗?如果您想包含多个子字符串怎么办? Sensorsensor?甚至更好的是,在不考虑大小写的情况下进行搜索
    • 也是*.c 只搜索当前目录中存在的文件还是子目录中存在的文件等等?我正在寻找后者的东西
    • *.c 仅匹配当前目录中的文件。就像评论说的那样,调整以匹配您的需要。 /[Ss]ensor/ 用于匹配传感器和传感器。
    • 搜索也可以通过递归方式遍历子目录?
    猜你喜欢
    • 2016-03-23
    • 2014-06-19
    • 2014-10-30
    • 2021-07-26
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多