Skip to main content
2 of 7
edited tags
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265

When using AWK with a variable pattern, can't get IF ELSE statement working

The AWK statement uses the variable $ip to search each line, but I need to print the variable $ip NOT FOUND in the ELSE statement for ips that are not found in the config.

Can't get it to work with IF ELSE, to reuse the $ip variable in the ELSE statement, so it would print 999.999.999.999 NOT IN CONFIG!

also, if possible to not use redundant getline getline getline's, like maybe a way to just skip 3 lines?

 declare -a iplist=(
 "192.168.0.10" 
 "192.168.0.20 
 "192.168.0.30" 
 "999.999.999.999"
)

for ip in "${iplist[@]}"; do

awk "/$ip/"'{print $0; getline; getline; getline; print $0; print "-----"}' /home/user/D1/config
 
done


config file contents:

ip=192.168.0.10
mask=255.255.255.0
allow=on
text=off
path=/home/user/D1/test/server1
-----
ip=192.168.0.20
mask=255.255.255.0
allow=on
text=off
path=/home/user/D1/test/server1
-----
ip=192.168.0.30
mask=255.255.255.0
allow=on
text=off
path=/home/user/D1/test/server1
-----


DESIRED OUTPUT:

ip=192.168.0.30
path=/home/user/D1/test/server1
-----
ip=192.168.0.30
path=/home/user/D1/test/server1
-----
ip=192.168.0.30
path=/home/user/D1/test/server1
-----
ip=192.168.0.30
path=/home/user/D1/test/server1
-----
ip=999.999.999.999 NOT IN CONFIG
-----