At the moment I am working in AIX box where sed -i is not available.
My requirement is as below
Two files file1 and file2. file1 contains the IP address, its count. file2 contains the Hostname and its corresponding IP address. I would like get the IP address replaced with the apt Hostname in file1.
But unfortunately sed -i is not present in my AIX box, please shoot me an alternative.
Also if the IP address is not found in file2, that row should be left untouched in file1.
Doubtless you can simplify the code further, i.e. by using awk to read both files and substitute the correct values. You would then write this to a temporary file and copy that over the original.
awk 'NR == FNR {
# while reading the fist input file
# store the names in an array
# keyed by the IPs
iton[$1] = $2
next
}
# if the IP has an associated name
# assign its value to $1
# otherwise just return the record
# 1 -> true -> print $0
($1 in iton && $1 = iton[$1]) || 1
' file2 file1 > _tmp_file_ &&
mv -- _tmp_file_ file1