仅当第一个模式存在于特定类型的文件上时,才应使用grep第二个模式

时间:2023-01-30 14:02:51

I have lots of .ini files which configures certain properties for various projects.

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

I want to filter for PROJECT_A in the ini file first and if that matches, then i want to filter the second pattern CONFIG_B. Since CONFIG_B is a property of PROJECT_A...X, i want to grep only the files which contains the PROJECT_A settings and CONFIG_B is also present. I know it is bit challenging, but if i can narrow down ini files with both PROJECT_A and CONFIG_A is present, i can manually inspect them to minimum list. I have 1000 files like this :-(

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

Typical Config is like this

典型配置是这样的

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

Expected out:-

期待: -

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

Results : file1.ini, file3.ini

结果: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

Since i get the output like above, im filtering only the lines containing .ini find . -name *.ini -exec grep -w PROJECT_A {} \; -print | grep ini

由于我得到上面的输出,我只过滤包含.ini的行。 -name * .ini -exec grep -w PROJECT_A {} \; -print | 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

how can i grep one line at a time for pattern CONFIG_A now

我怎么能一次为一个模式CONFIG_A grep一行

I understand i can write this to a file and read a line at a time, but i want a efficient way to do this.

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

Please help with your suggestions.

请帮助您的建议。

4 个解决方案

#1


1  

Saying:

他说:

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

would list files that contain both PROJECT_A and CONFIG_A.

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

Using the -q option for grep would evaluate to true only if the specified pattern existed in the file.

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

#2


0  

If you are looking for files where CONFIG_B occurs only in the stanza for proj:PROJECT_A, something like this?

如果您正在寻找仅在proj:PROJECT_A的节中发生CONFIG_B的文件,这样的话?

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

... or with the "real" values from the comment below,

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

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

#3


0  

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

How it works ?

怎么运行的 ?

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.

Hope it helps !!!

希望能帮助到你 !!!

#4


0  

What about this neat awk solution:

这个整洁的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

#1


1  

Saying:

他说:

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

would list files that contain both PROJECT_A and CONFIG_A.

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

Using the -q option for grep would evaluate to true only if the specified pattern existed in the file.

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

#2


0  

If you are looking for files where CONFIG_B occurs only in the stanza for proj:PROJECT_A, something like this?

如果您正在寻找仅在proj:PROJECT_A的节中发生CONFIG_B的文件,这样的话?

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

... or with the "real" values from the comment below,

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

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

#3


0  

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

How it works ?

怎么运行的 ?

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.

Hope it helps !!!

希望能帮助到你 !!!

#4


0  

What about this neat awk solution:

这个整洁的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