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.

1
  • 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
    CommentedSep 26, 2017 at 19:09

2 Answers 2

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.

2
  • Thanks I did not know comm before. How to skip blank lines?CommentedSep 26, 2017 at 19:32
  • @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm.
    – Kusalananda
    CommentedSep 26, 2017 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 

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.