4

Lets say I have two lists:

guests-2016.txt:

Peter
Michael
Frank
Dirk

guests-2017.txt:

Mark
Michael
Dirk
Lilly

How may I create two new lists of guests

  1. Guests that were in guests-2016.txt but are not in guests-2017.txt (former_guests.txt)

  2. Guests that were not in guests-2016.txt but are in guests-2017.txt now (new_guests.txt)

Blank lines should be ignored. Only standard utilities should be used.

My idea would be to use diff and do some post processing.

  • 6
    Check out the manual page for comm. Then add what you have tried, and explain how it did not work as you intended. – DopeGhoti Sep 26 '17 at 19:09
3

Given two sorted files, comm would do this for you.

See the combinations of the -2 -3 and -1 -3 command line options, for example.

  • Thanks I did not know comm before. How to skip blank lines? – NoobieNoob Sep 26 '17 at 19:32
  • @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm. – Kusalananda Sep 26 '17 at 19:41
1

Check, does it do the job. I can add an explanation, if you are needed.

awk '
/^$/{next}
FNR == NR {guest_2016[$1] = 1}
FNR != NR {
    if(!guest_2016[$1])
        print $1 > "new_guests.txt" 
    delete guest_2016[$1];
}
END {
    for(i in guest_2016)
        print i > "former_guests.txt"
}' guests-2016.txt guests-2017.txt

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.