AWK: replace single positional character given variables

I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk.

Story: I'm automating the (re)configuration of network interfaces, including hard-coding of speed/duplex, using the /kernel/drv/.conf files, through an interactive script. The script asks the user which interface needs to be configured. If the user enters e1000g, then the script gets the interface instance from that input by simply awking the echo of the variable and using the g as the field delimiter. e1000g interfaces are hard-coded using the /kernel/drv/e1000g.conf file. In this file, the default line that sets all e1000g interfaces sets them all to autoneg, and shows as:

ForceSpeedDuplex=7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7;

Each number 7 represents an interface from e1000g0 to e1000g15.

So, if for example, the user chooses to set e1000g2 to 100 full duplex, the 3rd 7 in the string needs to be changed to 4, which would hardcode 100 full duplex. The setting is going to be either 7(autoneg for gig interfaces) or 4 (100 full duplex) in all cases

My solution was to simply create an array with the 16 7s, then depending on the variables I get for speed and interface instance, if needed, change that array member to 4, then use the array in a sed command to replace the string of sevens to the string with the new setting. So it looks something like this (assume user entered e1000g2 as INPUT1 and 100 as INPUT2) :

# 
#Declare the array at the beginning of the script
set -A INT 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

# Next code is all in a function that does a few more things including input 
# checks

if (( ${INPUT2} < 101 )); then
   NIC=$(echo ${INPUT1} | awk -F"g" '{print $2}')
   INT[${NIC}]=4
   sed "s/7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7/${INT[0]},${INT[1]},(and on and on 16 times),..../" e1000g.conf
fi
# if $INPUT is bigger than 101, then no changes are needed, so just one if
# statement suffices

This works just fine, but the loooooong sed command is killing me :slight_smile: .
I would like to find a way to contract that line.. I looked into awk, but I just don't "see" the solution.

So, if anyone can come up with a terser command...

something like this? It will need to get 'massaged' to fit the wrapper script, but it's a start.
idx - index into array of 7-s (0-based - adding 1 as awk's arrays are 1-based)
val - value to assign to indexed cell.

echo '7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7' | nawk -F, -v idx=2 -v val=4 '$(idx+1)=val' OFS=','

Try this:

awk -F"=|," 'BEGIN { printf "Enter the settings delimited by a space: "
getline input < "-"
split(input,line," ")
}
/ForceSpeedDuplex=/ && line[2]<101 {
  split(line[1],n,"g")
  $(n[2]+1)=4
}
{print}' e1000g.conf

Regards

I like that. Thanks. I am not that great at awk, and I just couldn't visualize it.