1

I desire to compute the difference of two lists:

$ pip list > package1.md
Package                            Version  
---------------------------------- ---------
alabaster                          0.7.10   
anaconda-client                    1.6.9    
anaconda-navigator                 1.7.0    
anaconda-project                   0.8.2    
appnope                            0.1.0    
 ....

$ pip3 list > package2.md
Package                            Version  
---------------------------------- ---------
alabaster                          0.7.10   
anaconda-client                    1.6.9    
anaconda-navigator                 1.7.0    
anaconda-project                   0.8.2    
appnope    
...

To find the package installed in package1 but not in package 2.

How to accomplish such a task?

  • Fix your tags to remove bash. This is not a bash question, should be in text processing tag – Inian May 3 '18 at 10:15
  • you can use pip list --format=freeze to not have the header lines in the output; also you can easily store the freezed output to a file and install from the file later, via pip install -r file. – hoefling May 5 '18 at 10:13
2

With comm command:

comm -23 <(sort package1.md) <(sort package2.md)
| improve this answer | |
  • Is it guaranteed to do the right thing even though the data is not strictly speaking sorted? I've tested it, and it works, but since comm requires sorted input, I'm not quite sure why it works and whether it's guaranteed to work. – Kusalananda May 3 '18 at 10:31
  • 1
    @Kusalananda, it simply works cause pip list sorts the list, i.e. it does a part of work for us in this case – RomanPerekhrest May 3 '18 at 10:34
  • This list is sorted, but the input to comm is not. The first two lines (the header) is not in lexicographical order (- comes before P). – Kusalananda May 3 '18 at 10:35
  • Looking at the implementations of comm I have access to, it seems that it ought to work as long as the lines that are not sorted are identical in both files and as long as these lines are compared with each other and not otherwise offset from each other in the two files. – Kusalananda May 3 '18 at 10:46
  • 1
    @Kusalananda, yes, I've just played with some offsets and got comm: file 1 is not in sorted order.; an offset indeed matters. Updated – RomanPerekhrest May 3 '18 at 10:50
0

Use diff(1). You might want to preprocess with sort(1), and perhaps use tail(1) to cut off headers.

| improve this answer | |

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.