awk — File Diffs & Set Operations
Lines in file1 not in file2 (subtract)
awk 'NR==FNR {a[$0]; next} !($0 in a)' file2 file1
Lines common to both files (intersection)
awk 'NR==FNR {a[$0]; next} $0 in a' file2 file1
Symmetric difference — lines in either but not both
awk 'NR==FNR {a[$0]=1; next} {if($0 in a) a[$0]=0; else print} END{for(k in a) if(a[k]) print k}' file1 file2
Compare expected vs actual host lists — find extras
awk 'NR==FNR {expected[$0]; next} !($0 in expected) {print "EXTRA:", $0}' expected_hosts.txt actual_hosts.txt
Compare expected vs actual host lists — find missing
awk 'NR==FNR {actual[$0]; next} !($0 in actual) {print "MISSING:", $0}' actual_hosts.txt expected_hosts.txt
Interleave lines from two files
awk 'NR==FNR {a[FNR]=$0; max=FNR; next} {print a[FNR]; print}' file1 file2