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

File hosts_sorted   REF_UP-4   REF_UP-5   REF_UP-6   REF_UP-7   REF_UP-8   REF_UP-9   REF_UP-10   REF_UP-11

File hosts_new_sorted   REF_UP-4-new   REF_UP-5-new   REF_UP-6-new   REF_UP-7-new   REF_UP-8-new   REF_UP-9-new   REF_UP-11-new

And my expected output

<   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 '19 at 12:29
  • It also compares the second column (hostname in /etc/hosts/) – Philipp Birkl Apr 24 '19 at 12:31
  • what about if you had a host which only and only exist in host_new_sorted like 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 '19 at 15:02

Try this,

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

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

| improve this answer | |
  • 1
    It works only in one direction. That's enough? – pLumo Apr 24 '19 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.