I have 3 text files. I want to search file3
for a string in file2
and replace it with a string in file1
in found. I need to append a custom tag from file1
to the end of the string in file3
, replacing the partial string found from file2
.
file3
aws ec2 create-tags --region us-east-1 --resourcesi-XXXXX --tags Key=Developer Name,Value=XXXXX Key=Resource Group,Value=arn:aws:iam::XXXXX:root
aws ec2 create-tags --region us-east-1 --resourcesi-XXXX --tags Key=Developer Name,Value=XXXXX Key=Resource Group,Value=arn:aws:iam::XXXXX:user/user
file2
arn:aws:iam::XXXXX:root
arn:aws:iam::XXXXX:user/user
file1
my_custom_tag_1
my_custom_tag_2
Desired output:
aws ec2 create-tags --region us-east-1 --resourcesi-XXXXX --tags Key=Developer Name,Value=XXXXX Key=Resource Group,Value=my_custom_tag_1
aws ec2 create-tags --region us-east-1 --resourcesi-XXXX --tags Key=Developer Name,Value=XXXXX Key=Resource Group,Value=my_custom_tag_2
I've tried loading the lines from the file into an array and including the index in a sed
replace.
sed "s|${file2array[0]}|${file1array[0]}|g" file3.txt
But this returns a "no previous regular expression" error. I've also tried writing the array indexes to unique variables with a for loop and using the same approach above with the variables
sed "s|$var2|$var1|g" file3.txt
This also fails
Interestingly,
sed "s|${file2array[0]}|customtext}|g" file3.txt
fails but
sed "s|customtext|${file1array[0]}|g" file3.txt
succeeds.
Any help is greatly appreciated. Been working on this for dozens of hours now.