2

So basically I've a file that contains the below Data.txt

<IP Address1>, 10, 23, <GW IP1>
FINAL INPUT.45.324
<IP Address2>, 40, 33, <GW IP2>

Another file that has values for each TAG as specified below info.txt

<IP Address1>10.155.120.20
<GW IP1>10.155.120.30
<IP address2>10.30.123.30
<GW IP2>10.30.123.1

Would like the final to look like this (file.txt)

10.155.120.20, 10, 23, 10.155.120.30
FINAL INPUT.45.324
10.30.123.30, 40, 33, 10.30.123.1

Trying to find example but I'm unable to figure it out

0

The thing that you are looking for is called template engine. I made extremly simple not reliable one for you. It's based on sed, and is probably extremely fragile.

Note that you has <IP address2>10.30.123.30 instead of <IP Address2>10.30.123.30 so my script didn't (did) work as expected. The full code is below:

#!/usr/bin/env bash
cat <<EOF >file_1
<IP Address1>, 10, 23, <GW IP1>
FINAL INPUT.45.324
<IP Address2>, 40, 33, <GW IP2>
EOF
cat <<EOF >file_2
<IP Address1>10.155.120.20
<GW IP1>10.155.120.30
<IP Address2>10.30.123.30
<GW IP2>10.30.123.1
EOF
cp file_1 file_final
while read to_split; do
    echo "to_split: $to_split"
    arg1="${to_split%>*}>" # > because it is removed
    arg2="${to_split#*>}"
    echo "1: $arg1"
    echo "2: $arg2"
    sed -i  "s/$arg1/$arg2/g" file_final
done < file_2

The script is self contain so you can try it on your own :).

The thing is that I cannot tell that this is great solution. The real solution would be use the some template engine like Jinja2.

0

How far would

awk  '
NR==FNR         {T[$1 FS] = $2
                 next
                }
                {for (i=1; i<=NF; i++) if ($i in T) $i = T[$i]
                }
1
'
FS=">" file2 FS=", " OFS=", " file1
10.155.120.20, 10, 23, 10.155.120.30
FINAL INPUT.45.324
10.30.123.30, 40, 33, 10.30.123.1

get you? It collects the replacement values from file2 into an array indexed by the label, and then, in the data file1, checks each field for existence in the array, and replaces it if applicable. I replaced the lower case "address" by upper case, btw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.