Calmcode - cool cli: grep

grep

1 2 3 4 5 6

Installation

If you're on a mac the should already be installed. Just in case though, you can install the tool via;

brew install grep

On many linux distributions it should also already be installed.

Usage

The grep command can be used to filter through general stdout from the terminal. But in particular we will use it to query the installed python packages via pip freeze.

You can use it to find the installed version of pandas via;

pip freeze | grep pandas

You can also use it to find any packages that contain the substring "num".

pip freeze | grep num

You should be aware that grep is case sensitive. That means that these two commands should be expected to yield different outputs.

pip freeze | grep Sphi
pip freeze | grep sphi

If you want to run the command independant of capitalisation, you need to add the -i flag.

pip freeze | grep -i sphi

Finally, you should remember that the standard regex | character (which means "or") needs to be escaped. You typically don't want to search for the string "num|sci". Instead, you'd want grep to search for "num" or "sci". The following command shows an example;

pip freeze | grep -i 'num\|sci'