I am working on a shell script in AIX which has some code to update certain values in a text file. The file is the format as shown below:
# cat $FILE
instance1 13 16
instance2 14 12
instance4 58 76
instance15 44 91
instance102 31 10
instance112 12 38
I am trying to search by instance name and replace the values in column 2 and column 3 with new values I was able to do it using perl as follows:
# for i in `cat $FILE|awk '{print $1}'`;do
# perl -pi -e "s/`grep -i $i $FILE|awk '{print $2}'`/$NEW_VAL_2/ if /$i/" $FILE
# perl -pi -e "s/`grep -i $i $FILE|awk '{print $3}'`/$NEW_VAL_3/ if /$i/" $FILE
# done
This worked but then I realized that it is replacing every occurrence of the value. For example, in the last row it is replacing the value in column 2 but also the last two characters of the instance name.
Example:
# i = instance112
# NEW_VAL_2 = 99
# perl -pi -e "s/`grep -i $i $FILE|awk '{print $2}'`/$NEW_VAL_2/ if /$i/" $FILE
Output:
instance199 99 38
How can I search for the row by instance name and replace only the values in a particular column?