grep and find¶
grep¶
grep finds and prints lines in files that match a regular expression. In the following example, we search for the string Python:
[1]:
!grep Python ../index.rst
IPython
`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
advanced Python interpreter that has now grown into an extensive project
Today, IPython is not only an interactive interface to Python, but also offers a
number of useful syntactic additions for the language. In addition, IPython is
* `Miki Tebeka - IPython: The Productivity Booster
The option -w limits the matches to the word boundaries so that IPython is ignored:
[2]:
!grep -w Python ../index.rst
`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
advanced Python interpreter that has now grown into an extensive project
Today, IPython is not only an interactive interface to Python, but also offers a
-n shows the line numbers that match:
[3]:
!grep -n -w Python ../index.rst
8:`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
9:advanced Python interpreter that has now grown into an extensive project
11:Today, IPython is not only an interactive interface to Python, but also offers a
-v inverts our search
[4]:
!grep -n -v "^ " ../index.rst
1:.. SPDX-FileCopyrightText: 2020 cusy GmbH
2:..
3:.. SPDX-License-Identifier: BSD-3-Clause
4:
5:IPython
6:=======
7:
8:`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
9:advanced Python interpreter that has now grown into an extensive project
10:designed to provide tools for the entire life cycle of research computing.
11:Today, IPython is not only an interactive interface to Python, but also offers a
12:number of useful syntactic additions for the language. In addition, IPython is
13:closely related to the `Jupyter project <https://jupyter.org/>`_.
14:
15:.. seealso::
18:
19:.. toctree::
23:
grep has lots of other options. To find out what they are, you can type:
[5]:
!grep --help
usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
In the following example we use the -E option and put the pattern in quotes to prevent the shell from trying to interpret it. The ^ in the pattern anchors the match to the start of the line and the . matches a single character.
[6]:
!grep -n -E "^.Python" ../index.rst
5:IPython
find¶
find . searches in this directory whereby the search is restricted to directories with -type d.
[7]:
!find .. -type d
..
../mypackage
../unix-shell
../unix-shell/.ipynb_checkpoints
With -type f the search is restricted to files.
[8]:
!find . -type f
./index.rst
./shell-variables.ipynb.license
./sorted-length.txt
./create-delete.ipynb
./grep-find.ipynb.license
./create-delete.ipynb.license
./length.txt
./file-system.ipynb
./pipes-filters.ipynb
./shell-variables.ipynb
./pipes-filters.ipynb.license
./file-system.ipynb.license
./grep-find.ipynb
With -mtime the search is limited to the last X days, in our example to the last day:
[9]:
!find . -mtime -1
.
./sorted-length.txt
./create-delete.ipynb
./length.txt
./file-system.ipynb
./pipes-filters.ipynb
./.ipynb_checkpoints
./grep-find.ipynb
With -name you can filter the search by name.
[10]:
!find .. -name "*.rst"
../index.rst
../unix-shell/index.rst
../extensions.rst
../start.rst
Now we count the characters in the files with the suffix .rst:
[11]:
!wc -c $(find .. -name "*.rst")
923 ../index.rst
540 ../unix-shell/index.rst
2229 ../extensions.rst
820 ../start.rst
4512 total
It is also possible to search for a regular expression in these files:
[12]:
!grep "ipython.org" $(find .. -name "*.rst")
../index.rst:`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
Finally, we filter out all results whose path contains ipynb_checkpoints:
[13]:
!find . -name "*.ipynb" | grep -v ipynb_checkpoints
./create-delete.ipynb
./file-system.ipynb
./pipes-filters.ipynb
./shell-variables.ipynb
./grep-find.ipynb