I would like to know if it's possible to do the following thing only using awk: I am searching some regex in a file Fand I want to replace the string (S1) that matches the regex by another string (S2). Of course, it's easy to do that with awk. But ... my problem is that the value of S2 has to be obtained from another file that maps S1 to S2.
Example :
file F:
abcd 168.0.0.1 qsqsjsdfjsjdf
sdfsdffsd
168.0.0.2 sqqsfjqsfsdf
my associative table in another file
168.0.0.1 foo
168.0.0.2 bar
I want to get:
this result:
abcd foo qsqsjsdfjsjdf
sdfsdffsd
bar sqqsfjqsfsdf
Thanks for help !
edit: if my input file is a bit different, like this (no space before IP address):
file F:
abcd168.0.0.1 qsqsjsdfjsjdf
sdfsdffsd
168.0.0.2 sqqsfjqsfsdf
i can't use $1, $2 variables and search in the associative array. I tried something like that (based on birei proposition) but it did not work :
FNR < NR {
sub(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/, assoc [ & ] );
print
}
Is there a way to search the matched string in the associative array (assoc[ & ] seems to be not valid) ?
BEGINsection and query it then with your awk script. E.g.,BEGIN { t[x1] = y; t[x2] = y; ... } { if ($1 in t) ...; }