Skip to main content
added 26 characters in body
Source Link

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /home/user/D1/config

### BELOW WORKS - But, need the IF ELSE Statement ###
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.3010
path=/home/user/D1/test/server1
-----
ip=192.168.0.3020
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/testdifferent-path-than-line-above/server1
-----
ip=999.999.999.999 NOT IN CONFIG
-----

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /home/user/D1/config

### BELOW WORKS - But, need the IF ELSE Statement ###
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
-----

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /home/user/D1/config

### BELOW WORKS - But, need the IF ELSE Statement ###
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.10
path=/home/user/D1/test/server1
-----
ip=192.168.0.20
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/different-path-than-line-above/server1
-----
ip=999.999.999.999 NOT IN CONFIG
-----
added 160 characters in body
Source Link

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /home/user/D1/config 

### BELOW WORKS - But, need the IF ELSE Statement ###
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
-----

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /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
-----

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /home/user/D1/config 

### BELOW WORKS - But, need the IF ELSE Statement ###
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
-----
deleted 12 characters in body
Source Link
muru
  • 77.9k
  • 16
  • 212
  • 318

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /home/user/D1/config
 
done

 
config file contents:

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:

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
-----

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /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
-----

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/" '{if {print $0; getline; getline; getline; print $0; print "-----"} else {print "/$ip/" "NOT FOUND"}' /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
-----
added 38 characters in body
Source Link
Loading
added 1 character in body
Source Link
user313992
user313992
Loading
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265
Loading
Source Link
Loading