Reputation: 23
I am trying to evaluate two different files (.tf files) to check if the resource names are the same in a bash script. I know that in those files resources are declared as such:
resource "service_name" "resource_name" {
#resource config
}
One approach would be something like:
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
From a test file (file1) an example input would be:
resource "service_name" "resource_name_AA" {
#resource config
# policy_config = << POLICY
{
policy_definition
} POLICY
}
From a test file (file2) an example input would be:
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
}
The desired output would be (diff_file):
resource "service_name" "resource_name_BB" {
#resource config
# policy_config = << POLICY
{
policy_definition
} POLICY
}
Upvotes: 2
Views: 93
Reputation: 6779
This might do the job. I guess as per the code you showed, the contents of the resource also need to to printed in cases of mismatch. If only difference needs to be pointed out, diff
is better and enough. Anyways I still like writing awk
(still a bit new, learning) scripts, so wrote one.
#! /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
UPDATE:
#! /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
#! /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
Upvotes: 0
Reputation: 3168
I think I would try to keep it simpler than that:
grep 'resource' file1 > file1_resources.txt
grep 'resource' file2 > file2_resources.txt
diff file{1,2}_resources.txt
if the word "resource" shows up in different contexts, then you could use a regexp grep instead:
egrep "resource.*\{" fileX
Upvotes: 2