Monitoring changes in file with bash

0

I'm running DHCP server on local network which calls this script every few seconds:

dumpleases | awk '{print $2}' > leasesnow     #get list of all IPs leased in a file, one per line
ADDED=`awk 'NR==FNR{a[$1]=$2;next} !($1 in a) { print $2 }' leasesthen leasesnow`     #try to extract only new leases to variable
print $ADDED
checkin.sh $ADDED    #do something with new devices on network
cp -f leasesnow leasesthen

I've also tried using diff, but it looks even more complex than awk, because it requires to filter out changed lines from output first.

What's wrong: $ADDED never receives correct (even any) values, which means awk is not working as intended. Can you please explain what's wrong? I'm quite new to awk. Thanks.

ZuOverture

Posted 2015-06-12T16:44:37.543

Reputation: 120

Answers

0

I know just the tool you're looking for! comm.

The current ips file old.txt.

68.180.194.242
68.180.194.243
69.147.112.168
69.147.112.169
87.248.122.141
87.248.122.142
209.131.41.48
209.131.41.49
216.39.58.17
216.39.58.18
216.39.58.78
217.12.1.124
217.12.1.125
217.146.191.18
217.146.191.19
87.248.125.48
87.248.125.49
98.136.63.35

The new ips file new.txt.

68.180.194.242
68.180.194.243
69.147.112.168
69.147.112.169
87.248.122.141
87.248.122.142
209.131.41.48
209.131.41.49
216.39.58.17
216.39.58.18
216.39.58.78
217.12.1.124
217.12.1.125
217.146.191.18
217.146.191.19
87.248.125.48
87.248.125.49
98.136.63.35
68.142.243.103
98.139.134.96
98.139.134.97
98.139.134.98
98.139.134.99
173.224.120.84
37.193.134.104
178.65.210.178
31.130.202.80
94.228.44.113
161.69.47.4
210.75.14.146

The output of comm -13 <(sort old.txt) <(sort new.txt)

161.69.47.4
173.224.120.84
178.65.210.178
210.75.14.146
31.130.202.80
37.193.134.104
68.142.243.103
94.228.44.113
98.139.134.96
98.139.134.97
98.139.134.98
98.139.134.99

The flag -1 suppresses the lines unique to the first file, the flag -3 suppresses the lines in both files, and the comm utility requires that the files be in sorted order which is they're both being sorted.

Akinos

Posted 2015-06-12T16:44:37.543

Reputation: 306