本文介绍了awk中找到,如果多个常见模式之间存在特定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能完全得到我的报表工作,我所需要的。

I cant quite get my statements working as I need.

我需要找到如果每个称为集团的模式包含一个配置文件中的行FUNEnable。如果它的present然后后添加一行在文件中,如果没有则打印警告画面。的可以是如下所示的任何数量的被称为组模式:

I need to find if each of the patterns called Group contain the line FUNEnable within a config file. If its present then add a line after it within the file, if not then print a warning to screen. The could be any number of patterns called group as shown below:

示例文件

#config file
<Some config>
More config
</Some config>

#comment about FUNEnable
Other config things

<Group small.group>
Something here
FUNEnable
Funfile /someplace/afunfile
</Group>

<Group medium.group>
More stuff here
Funfile /someplace/afunfile
</Group>

第一次尝试:

cat configfile.conf | awk '/^<Group/,/^<\/Group>/' | grep -q ^'FUNEnable' || print "WARNING";

这个伟大的工程,如果两个模式都没有SSLEnable,但如果一个人有SSLEnable present那么它不打印警告。我显然需要建立某种形式循环到它,但不能肯定。

This works great if both patterns do not have SSLEnable but if one has SSLEnable present then it does not print the warning. I obviously need to build some form of loop into it but not quite sure

cat configfile.conf | awk -F /\n '/^<Group/,/^<\/Group>/ { if ($1 == "FUNEnable") {print $1 "\nANOTHER LINE"} else { print "WARNING"}}';

这个犯规很做我所需要的。

This doesnt quite do what i need.

任何指针将是巨大的。

推荐答案

这可能工作:

awk -F"\n" '{for (i=1;i<=NF;i++) if ($i~/^[ \t]*FUNEnable/) {f=1;$i=$i"\nNew line"}} 1; END  {if (!f) print "not found"} ' RS="" OFS="\n" ORS="\n\n" configfile.conf
#config file
<Some config>
More config
</Some config>

#comment about FUNEnable
Other config things

<Group small.group>
Something here
FUNEnable
New line
Funfile /someplace/afunfile
</Group>

<Group medium.group>
More stuff here
Funfile /someplace/afunfile
</Group>

如果它发现开头 FUNEnable A线后它增加了一个新的生产线。结果
如果它没有找到它打印未找到在文件末尾。

If it finds a line starting with FUNEnable it adds a new line after it.
If it's not found it prints not found at end of file.

一些更具可读性:

awk '
    {
    for (i=1;i<=NF;i++) 
        if ($i~/^[ \t]*FUNEnable/) {
            f=1
            $i=$i"\nNew line"}} 
1
END  {
    if (!f) print "not found"} 
' FS="\n" RS="" OFS="\n" ORS="\n\n" configfile.conf

这篇关于awk中找到,如果多个常见模式之间存在特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:35