[SCRIPT] read and substitution

Hi all,

Actually I have a huge bash file like:

port1=$PORT && if [ $port -eq "$NUM"]; then .....
....stuff
port2=$PORT && if [ $port -eq "$NUM"]; then .....
...stuff
port3=$PORT && if [ $port -eq "$NUM"]; then .....
...stuff
port4=$PORT && if [ $port -eq "$NUM"]; then .....
...stuff
port5=$PORT && if [ $port -eq "$NUM"]; then .....

I would like to change the $port inside the square brackets accordly to the beginning of line, by getting:

port1=$PORT && if [ $port1 -eq "$NUM"]; then .....
....stuff
port3=$PORT && if [ $port3 -eq "$NUM"]; then .....
...stuff
port8=$PORT && if [ $port8 -eq "$NUM"]; then .....
...stuff
port66=$PORT && if [ $port66 -eq "$NUM"]; then .....
...stuff
port5=$PORT && if [ $port5 -eq "$NUM"]; then .....

I was thinking to do that with sed but not able to work out a solution.
Any idea please?
Thanks in advance

$ nawk -F"[= ]" ' {if($1~/port/) {$6="\$"$1; print $0} else {print $0}} ' test                                                                     
port1 $PORT && if [ $port1 -eq "$NUM"]; then .....
....stuff
port2 $PORT && if [ $port2 -eq "$NUM"]; then .....
...stuff
port3 $PORT && if [ $port3 -eq "$NUM"]; then .....
...stuff
port4 $PORT && if [ $port4 -eq "$NUM"]; then .....
...stuff
port5 $PORT && if [ $port5 -eq "$NUM"]; then .....
1 Like

Thanks a lot