A bit late answer... :)
In your code you calling the sed n-times. This is inefficient. Therefore me proposing different solution, using ed instead of the sed. (as in good old times 30 years ago in BSD 2.9 :) ).
For this, approach:
- first creating commands for the
ed
- executing them in one editor invocation
# it is good practice not using UPPERCASE variables
# as theycould collide with ENV variables
names=(Skypper Lampart Shepard Ryan Dean Jensen)
file="config.txt"
#create an array of commands for the "ed"
declare -a cmd
for name in "${names[@]}"; do
cmd+=("/Name/s//$name/")
done
cmd+=(w q)
echo "=== [$file before] ==="
cat "$file"
echo "=== [commands for execution ]==="
printf "%s\n" "${cmd[@]}"
#execute the prepared command in the "ed"
printf "%s\n" "${cmd[@]}" | ed -s "$file"
echo "===[ $file after ]==="
cat "$file"
output from the above
=== [config.txt before] ===
1
2
Name 1
Name 2
Name 3
Name 4
Name 5
Name 6
=== [commands for execution ]===
/Name/s//Skypper/
/Name/s//Lampart/
/Name/s//Shepard/
/Name/s//Ryan/
/Name/s//Dean/
/Name/s//Jensen/
w
q
===[ config.txt after ]===
1
2
Skypper 1
Lampart 2
Shepard 3
Ryan 4
Dean 5
Jensen 6
a variant which replaces by the line-numbers
names=(Skypper Lampart Shepard Ryan Dean Jensen)
file="config.txt"
#create an array of commands for the "ed"
declare -a cmd
n=3
for name in "${names[@]}"; do
cmd+=("${n}s/.*/$name/")
let n++
done
cmd+=(w q)
echo "=== [$file before] ==="
cat "$file"
echo "=== [commands for execution ]==="
printf "%s\n" "${cmd[@]}"
#execute the prepared command in the "ed"
printf "%s\n" "${cmd[@]}" | ed -s "$file"
echo "===[ $file after ]==="
cat "$file"
output
=== [config.txt before] ===
1
2
Name 1
Name 2
Name 3
Name 4
Name 5
Name 6
=== [commands for execution ]===
3s/.*/Skypper/
4s/.*/Lampart/
5s/.*/Shepard/
6s/.*/Ryan/
7s/.*/Dean/
8s/.*/Jensen/
w
q
===[ config.txt after ]===
1
2
Skypper
Lampart
Shepard
Ryan
Dean
Jensen