how to use AWK command in for loop Linux scripting

Let’s say we Have 2 variables with set of values .would like iterate through one list and if it matches my criteria like the word contains Admin print that corresponding port as Admin port ex 7001

SERVER="AdminSrv AppSrv01 AppSrv02"
PORT="7001 7003 7005"

I got the word count from below

SERVERCOUNT=`echo $SERVER |wc -w`

Now trying to iterate with the given code snippet

for ((i=1;i<=$SERVERCOUNT;i++)) ; do
  SERVERNAME=`echo $SERVER |awk '{ print $i }'`
  echo $SERVERNAME
done

see below output when we execute above

[ec2-user@ip-172-31-14-154 ~]$ SERVER="AdminSrv AppSrv01 AppSrv02"
[ec2-user@ip-172-31-14-154 ~]$ PORT="7001 7003 7005"
[ec2-user@ip-172-31-14-154 ~]$ SERVERCOUNT=`echo $SERVER |wc -w`
[ec2-user@ip-172-31-14-154 ~]$ echo $SERVERCOUNT
3
[ec2-user@ip-172-31-14-154 ~]$ 
[ec2-user@ip-172-31-14-154 ~]$ 
[ec2-user@ip-172-31-14-154 ~]$ for ((i=1;i<=$SERVERCOUNT;i++)) ; do
>   SERVERNAME=`echo $SERVER |awk '{ print $i }'`
>   echo $SERVERNAME
> done
AdminSrv AppSrv01 AppSrv02
AdminSrv AppSrv01 AppSrv02
AdminSrv AppSrv01 AppSrv02
[ec2-user@ip-172-31-14-154 ~]$ 

This is not the expected output that we want . We would like to print only $1 $2 and $3 in a line but it is not giving correct result .

Thought of using escape and $ to print $i . let us see

for ((i=1;i<=$SERVERCOUNT;i++)) ; do
  SERVERNAME=`echo $SERVER |awk '{ print /$$i }'`
  echo $SERVERNAME
done

This gives me error .

[ec2-user@ip-172-31-14-154 ~]$ for ((i=1;i<=$SERVERCOUNT;i++)) ; do
>   SERVERNAME=`echo $SERVER |awk '{ print /$$i }'`
>   echo $SERVERNAME
> done
awk: cmd. line:1: { print /$$i }
awk: cmd. line:1:          ^ unterminated regexp
awk: cmd. line:1: { print /$$i }
awk: cmd. line:1:               ^ unexpected newline or end of string

awk: cmd. line:1: { print /$$i }
awk: cmd. line:1:          ^ unterminated regexp
awk: cmd. line:1: { print /$$i }
awk: cmd. line:1:               ^ unexpected newline or end of string

awk: cmd. line:1: { print /$$i }
awk: cmd. line:1:          ^ unterminated regexp
awk: cmd. line:1: { print /$$i }
awk: cmd. line:1:               ^ unexpected newline or end of string

[ec2-user@ip-172-31-14-154 ~]$ 

With AWK we can use additional variable and assign to it .

for ((i=1;i<=$SERVERCOUNT;i++)) ; do
  SERVERNAME=`echo $SERVER |awk -v j=$i '{ print $j }'`
  echo $SERVERNAME
done

this gives the expected output one at time in line .

[ec2-user@ip-172-31-14-154 ~]$ for ((i=1;i<=$SERVERCOUNT;i++)) ; do
>   SERVERNAME=`echo $SERVER |awk -v j=$i '{ print $j }'`
>   echo $SERVERNAME
> done
AdminSrv
AppSrv01
AppSrv02
[ec2-user@ip-172-31-14-154 ~]$ 

Lets get the word contains Admin and print the corresponding port

for ((i=1;i<=$SERVERCOUNT;i++)) ; do
  SERVERNAME=`echo $SERVER |awk -v j=$i '{ print $j }'`
  if [[ `echo $SERVERNAME | grep Admin |grep -v grep |wc -l` -eq 1 ]] ; then
 echo $PORT |awk -v j=$i '{ print $j }' 
  fi
done

lets run the above modified code to print the Admin port

[ec2-user@ip-172-31-14-154 ~]$ for ((i=1;i<=$SERVERCOUNT;i++)) ; do
>   SERVERNAME=`echo $SERVER |awk -v j=$i '{ print $j }'`
>   if [[ `echo $SERVERNAME | grep Admin |grep -v grep |wc -l` -eq 1 ]] ; then
>  echo $PORT |awk -v j=$i '{ print $j }' 
>   fi
> done
7001
[ec2-user@ip-172-31-14-154 ~]$ 

That’s it able to use AWK with in the For Loop successfully .

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *