0

I have to compare the IPs of two different /etc/hosts files and show a diff in side-by-side view.

File hosts_sorted

10.244.74.152   REF_UP-4
10.244.74.154   REF_UP-5
10.244.74.156   REF_UP-6
10.244.74.153   REF_UP-7
10.244.74.155   REF_UP-8
10.244.74.157   REF_UP-9
10.244.74.159   REF_UP-10
10.244.74.160   REF_UP-11

File hosts_new_sorted

10.244.74.152   REF_UP-4-new
10.244.74.154   REF_UP-5-new
10.244.74.156   REF_UP-6-new
10.244.74.153   REF_UP-7-new
10.244.74.155   REF_UP-8-new
10.244.74.157   REF_UP-9-new
10.244.74.160   REF_UP-11-new

And my expected output

< 10.244.74.159   REF_UP-10

I already sorted the entries by IP and used diff <(cut -f1 hosts_sorted) <(cut -f1 hosts_new_sorted). Howwever, this cuts out the hostnames which are needed. Is there a way to tell diff only to use the first column? However the comparison shall not include the second column.

  • In what way is diff hosts_sorted hosts_new_sorted not doing what you want? – Kusalananda Apr 24 at 12:29
  • It also compares the second column (hostname in /etc/hosts/) – Philipp Birkl Apr 24 at 12:31
  • what about if you had a host which only and only exist in host_new_sorted like 10.244.74.162 REF_UP-12-new? you don't want that line then as your accepted answer is only doing checking in one direction meaning that print those lines that that host only and only exist in host_sorted. – αғsнιη Apr 24 at 15:02
0

Try this,

awk 'FNR==NR{a[$1];next}!($1 in a)' hosts_new_sorted hosts_sorted
10.244.74.159   REF_UP-10

it will check the presence of the first column of hosts_sorted in hosts_new_sorted, prints if not available.

  • 1
    It works only in one direction. That's enough? – pLumo Apr 24 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.