0

我有很多 .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 find 的行。-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 一行

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

请帮助您的建议。

4

4 回答 4

1

说:

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

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

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

于 2013-09-19T16:29:50.853 回答
0
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.

希望能帮助到你 !!!

于 2013-09-19T16:44:14.570 回答
0

如果您正在寻找CONFIG_B仅出现在 for 节中的文件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 }' {} \;
于 2013-09-19T16:45:26.090 回答
0

这个整洁的 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
于 2013-09-19T16:45:45.200 回答