If file2 needs to be created from scratch:
awk -F: -v OFS='=' '{
split($1, a, "-")
print toupper(a[1])"_SERVICE", $2
}' file1 > file2
If file2 already has variables that need to be overwritten:
#! /usr/bin/env bash
awk -F: -v OFS='=' '{
if (FNR == NR) {
split($1, a, "-")
var[toupper(a[1])] = $2
} else {
split($0, a, "=")
split(a[1], b, "_")
if (var[b[1]] != "") {
print a[1], var[b[1]]
} else {
print
}
}
}' file1 file2 > file2_new
mv file2_new file2
Second snippet explaination:
1) awk loops through the input files line by line.
2) While doing so, $0 contains the value of complete line. $1, $2, $3… contain individual : delimited values. We asked : to be delimeter in arg -F:.
3) NR is a variable the contains the line number that it is currently on.
4) As we are inputting 2 files to awk, it's a little different. NR resets to 0 when we go to next file.
FNR is a variable that would keep on incrementing regardless of the file it's going through.
5) Conditional check FNR == NR means that we are on the first file.
Taking example from file1, let's say we were on line
mint-value-daemon:10/GH/KL-19GA1-2020
$0 = mint-value-daemon:10/GH/KL-19GA1-2020
$1 = mint-value-daemon
$2 = 10/GH/KL-19GA1-2020
6) In this conditional statement we use split() function to split $1 by - into an array named a.
So basically this line:
split($1, a, "-")
Gives us:
a[1] = mint
a[2] = value
a[3] = daemon
We use an associative array now, named var.
var[toupper(a[1])] = $2
This stores value of $2 in at the key MINT i.e.
var["MINT"] = 10/GH/KL-19GA1-2020
Going so, it stores value of every such element in file1.
When condition FNR == NR goes false i.e. we start traversing through 2nd file, we follow a similar approach.
Remember file separator is still :, and it is not the separator in file2.
So in else:
Let's say we were on line:
MINT_SERVICE=10/GH/KL-19GA1-2020 //value2 from file1
We use:
split($0, a, "=")
Which gives us:
a[1] = MINT_SERVICE
a[2] = 10/GH/KL-19GA1-2020 //value2 from file1
Then on a[1] we again use split based on _.
split(a[1], b, "_")
This gives us:
b[1] = MINT
b[2] = SERVICE
Now:
if (var[b[1]] != "") {
print a[1], var[b[1]]
} else {
print
}
In array var in which we stored values when FNR == NR was true,
We check if
var["MINT"] != ""
if this holds true,
print a[1], var[b[1]]
which prints:
MINT_SERVICE=10/GH/KL-19GA1-2020
The reason for = is the -v OFS='=' which is the separator of print.
in the else:
print
which is same as
print $0
i.e. if this value if not a key in var print it without any modification.
All this output goes to file2_new.
In the end we rename file2_new to file2.
_/-) work for you?VAR_NAME=with an empty value? If a value is already present, should it be updated based on File 1?