比较文件以获取特殊字符之前的字符串

时间:2019-08-22 17:01:42

标签: bash shell scripting

我试图评估两个不同的文件(.tf文件),以检查bash脚本中的资源名称是否相同。 我知道在那些文件中资源是这样声明的:

resource "service_name" "resource_name" {
 #resource config
}

一种方法是:

while read line
do
    if word_file1 == "resource"; then
        #save string in array1 before "{" character
    fi
    while read line
    do
        if word_file2 == "resource"; then
            #save string in array2 before "{" character
            if array1 == array2; then
                break
            else 
                # write diff to another file, including all config 
                # info between {} for the missing or different resource
            fi
        fi
    done < filename2
done < filename1

在测试文件(file1)中,示例输入为:

resource "service_name" "resource_name_AA" {
 #resource config
 # policy_config = << POLICY
 { 
     policy_definition

 } POLICY
}

在测试文件(file2)中,示例输入为:

resource "service_name" "resource_name_AA" {
 #resource config
 # policy_config = << POLICY
 { 
     policy_definition

 } POLICY
}

resource "service_name" "resource_name_BB" {
 #resource config
 # policy_config = << POLICY
 { 
     policy_definition

 } POLICY
}

所需的输出为(diff_file):

resource "service_name" "resource_name_BB" {
 #resource config
 # policy_config = << POLICY
 { 
     policy_definition

 } POLICY
}

2 个答案:

答案 0 :(得分:2)

我想我会尝试使其更简单:

 grep 'resource' file1 > file1_resources.txt 
 grep 'resource' file2 > file2_resources.txt 
 diff file{1,2}_resources.txt 

如果“ resource”一词出现在不同的上下文中,则可以使用regexp grep代替:

egrep "resource.*\{" fileX

答案 1 :(得分:0)

这可能会完成任务。我想根据您显示的代码,在不匹配的情况下也需要打印资源的内容。如果仅需要指出差异,则diff更好且足够。无论如何,我仍然喜欢编写awk(还是一个新的学习脚本),所以写了一个。

#! /bin/bash

awk '{
    if (FNR == NR)
    {
        if ($1 == "resource")
            resource_name=$3
        else
            contents[resource_name]=contents[resource_name]"\n"

        contents[resource_name]=contents[resource_name]$0
    }
    else
    {
        if (($1 == "}") && (flag == 1))
        {
            flag=0
            next
        }
        else
        if (($1 == "resource") && (contents[$3] != ""))
        {
            flag=1
            contents[$3]=""
            next
        }   

        if (flag == 1)
            next

        print
    }
} 
END {
    for (resource in contents) 
    {
        if (contents[resource] != "")
            print contents[resource]
    }
}
' file2 file1 > otherfile

更新:

#! /bin/bash

awk '{
    if (FNR == NR)
    {
        if ($1 == "resource")
            resource_name=$3
        else
            contents[resource_name]=contents[resource_name]"\n"

        contents[resource_name]=contents[resource_name]$0
    }
    else
    {
        if (($1 == "}") && (flag == 1))
        {
            flag=0
            next
        }
        else
        if (($1 == "resource") && (contents[$3] == ""))
        {
            flag=1
            contents[$3]=""
            next
        }   

        if (flag == 1)
            next

        print
    }
}' file1 file2 > same_resources

UPDATE-2:

#! /bin/bash

awk '{
    if (FNR == NR)
    {
        if ($1 == "resource")
            resource_name=$3
        else
            contents[resource_name]=contents[resource_name]"\n"

        contents[resource_name]=contents[resource_name]$0
    }
    else
    {
        if ($1 == "resource")
        {
            if (flag == 1)
            {
                flag=0
            }
            if (contents[$3] != "") 
            {
                flag=1
                contents[$3]=""
            }
        }   

        if (flag == 1)
            next

        print
    }
} 
END {
    for (resource in contents) 
    {
        if (contents[resource] != "")
            print contents[resource]
    }
}' file2 file1 > someotherfile
相关问题