1

Is it possible to use more than one input on Unix grep? Here is what I'm trying to do:

pip freeze list all installed python packages, but unfortunately also list it's dependencies

cat requirements/base.txt list my project packages

I've been trying to use something like pip freeze | grep $(cat requirements/base.txt) but without sucess.

Does anybody knows how to filter pip freeze to actually list only the packages I'm interested?

1

To make your command put, just put double quotes around the command substitution. For example, consider this base.txt file:

$ cat base.txt
simplegeneric==0.8.1
six==1.10.0
sympy==1.1

Without quotes, the command generates a series of errors. With quotes, it works just fine:

$ pip3 freeze | grep "$(cat base.txt)"
simplegeneric==0.8.1
six==1.10.0
sympy==1.1

Better yet, unless you are familiar with regular expression syntax, it is probably safer to use the -F option so that grep interprets each line in base.txt as a plain string. It makes no difference here but it may help avoid surprises in the future:

$ pip3 freeze | grep -F "$(cat base.txt)"
simplegeneric==0.8.1
six==1.10.0
sympy==1.1

Still better (hat tip: Nasir Riley), use the -f option to read the strings from a file without needing to use cat:

$ pip3 freeze | grep -Ff base.txt
simplegeneric==0.8.1
six==1.10.0
sympy==1.1
  • Ah, I was missing the double quotes! Thank you very much!! – Vini.g.fer Apr 16 at 21:24
  • 1
    The above command using grep -f is better as it avoids the useless use of cat. the -f switch tells grep to read the strings from the file directly so there's no need to use cat. – Nasir Riley Apr 16 at 21:28
  • @NasirRiley Very good. Answer updated with -f. – John1024 Apr 16 at 21:36

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.