【问题标题】:grep second pattern only if the first pattern exist on a specific type of file仅当第一个模式存在于特定类型的文件上时 grep 第二个模式
【发布时间】:2013-09-20 01:21:18
【问题描述】:

我有很多 .ini 文件,它们为各种项目配置某些属性。

我想首先过滤 ini 文件中的 PROJECT_A,如果匹配,那么我想过滤第二个模式 CONFIG_B。由于 CONFIG_B 是 PROJECT_A...X 的属性,我只想 grep 包含 PROJECT_A 设置的文件,并且 CONFIG_B 也存在。我知道这有点挑战性,但如果我可以缩小同时存在 PROJECT_A 和 CONFIG_A 的 ini 文件,我可以手动将它们检查到最小列表。我有 1000 个这样的文件 :-(

典型的配置是这样的

[F-Project:PROJECT_A]
stream-window-start=0
stream-window-end=0
network-feed=LIVE:
test-config=pdl tf_dms_hiab

预期结果:-

file1.ini
proj:PROJECT_A
cfg1:CONFIG_A
cfg1:CONFIG_B
cfg1:CONFIG_C

proj:PROJECT_B
cfg1:CONFIG_A
cfg1:CONFIG_C

file2.ini
proj:PROJECT_X
cfg1:CONFIG_A
cfg1:CONFIG_B
cfg1:CONFIG_C

proj:PROJECT_Y
cfg1:CONFIG_B
cfg1:CONFIG_C

file3.ini
proj:PROJECT_A
cfg1:CONFIG_B
cfg1:CONFIG_C

proj:PROJECT_B
cfg1:CONFIG_A

结果:file1.ini、file3.ini

find . -name *.ini -exec grep -w PROJECT_A {} \; -print | grep ini -exec grep CONFIG_A {} \;

[proj:PROJECT_A]
./PLATFORM/build/integration/suites/System_Maintenance_Suite/ini/Test_0621_1.ini

由于我得到了上面的输出,我只过滤包含 .ini 的行 寻找 。 -name *.ini -exec grep -w PROJECT_A {} \; -打印 | grep ini

./PLATFORM/build/integration/suites/System_Maintenance_Suite/ini/Test_0722_1.ini
./PLATFORM/build/integration/suites/System_Maintenance_Suite/ini/Test_0579_15.ini
./PLATFORM/build/integration/suites/System_Maintenance_Suite/ini/Test_0460_1.ini

我现在如何一次为模式 CONFIG_A grep 一行

我知道我可以将它写入文件并一次读取一行,但我想要一种有效的方法来做到这一点。

请帮忙提出您的建议。

【问题讨论】:

    标签: linux command-line grep


    【解决方案1】:

    说:

    find . -name *.ini -exec sh -c "grep -q PROJECT_A {} && grep -q CONFIG_A {} && echo {}" \;
    

    将列出同时包含 PROJECT_A 和 CONFIG_A 的文件。

    仅当文件中存在指定的模式时,对grep 使用-q 选项才会评估为true。

    【讨论】:

    • 感谢您的建议。确切的形式是 find 。 -name *.ini -exec sh -c "grep -q PROJECT_A {} && grep -q CONFIG_A {} && echo {}" \;,需要将 {} 传递给每个 grep 操作,否则它会永远挂起。
    • @rk.infotek.ltd 当然!感谢您发现这一点。没有文件名,grep 会做什么。
    【解决方案2】:

    如果您要查找的文件中 CONFIG_B 仅出现在 proj:PROJECT_A 的节中,类似这样?

    find . -type f -name '*.ini' -exec awk '
        /^proj:/ { prj=$1; next }
        /CONFIG_B/ && prj="proj:PROJECT_A" {
            print FILENAME; exit 0 }' {} \;
    

    ...或使用下面评论中的“真实”值,

    find . -type f -name '*.ini' -exec awk '
        /^F-Project:/ { prj=$1; next }
        /LIVE:/ && prj="F-Project:PROJECT_A" {
            print FILENAME; exit 0 }' {} \;
    

    【讨论】:

    • 嗨,我已经用 .ini 文件中的正确格式“Typical Config is like this”更新了问题。你给的那个给我抛出了 awk 错误。
    • 这是我按照您的指示尝试过的,如果我发现错误,请纠正我。 -type f -name '*.ini' -exec awk ' /^F-Project:/ { F-Project:$1;下一个 } /LIVE:/ && F-Project="F-Project:PROJECT_A" { 打印文件名;退出 0 }' {} \;
    • 不,prj 只是一个 awk 变量的名称;不要重命名。
    • awk: /^F-Project:/ { prj:$1;下一个 } /LIVE:/ && F-Project="F-Project:PROJECT_A" { 打印文件名;退出 0 } awk: ^ 语法错误
    • 感谢您的努力,它没有按预期工作。我在文件中没有任何 PROJECT_A 的“LIVE:”匹配。我尝试了 5 个不同的文件。对不起,我不会再测试了。感谢您的帮助。
    【解决方案3】:
    find . -name *.ini -exec sh -c "grep -q PROJECT_A {} && grep -q CONFIG_A {} && echo {}" \;
    

    它是如何工作的?

    find . -name *.ini <= filters the .ini files
    -exec <= executes the following command using the output from find one at a time
    sh -c <= accepts string input for the shell, we need this for executing multiple commands which follws that
    grep -q PROJECT_A {} <= quietly grep for PROJECT_A
    grep -q PROJECT_A {} && grep -q CONFIG_A {} && echo {} <= prints the filename if both the strings are matches, simple logical and on three commands.
    

    希望对你有帮助!!!

    【讨论】:

    • @JarrodRoberson 希望对您有所帮助
    【解决方案4】:

    这个简洁的 awk 解决方案怎么样:

    awk -vPR="PROJECT_A" -vCF="CONFIG_A" 'BEGIN{R="(" CF "|" PR ")"}
        {if($0 ~ R)d[FILENAME]+=1}
        END{for(i in d)if(d[i]>=2)print i}' file*
    file3.ini
    file1.ini
    

    【讨论】:

    • 前导通配符和尾随通配符是多余的。
    • @tripleee - 谢谢,在 if 语句 rk.infotek.ltd 之后还删除了多余的 {} - 非常奇怪的双重检查,似乎工作 bash --version GNU bash,版本 4.1.2(1) -release (x86_64-redhat-linux-gnu) GNU awk, 3.1.7
    • 我得到了 GNU bash,版本 4.1.2(1)-release (x86_64-redhat-linux-gnu),它是一样的 :-)
    猜你喜欢
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2017-10-28
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    相关资源
    最近更新 更多