Turn on line numbers while searching in files

Introduction

Due to “popular demand” I have added a feature to the scriptutil.ffindgrep() function of the scriptutil.py module: you can now instruct it to display the line numbers for the lines found (similar to grep -n).

Please note: the examples below operate on the django project source code tree as usual.

Examples

What follows is a brief demonstration of the scriptutil.ffindgrep() function showing how one can search with and without line numbers respectively.

I am first searching with line numbers turned off. The results are displayed on lines 10-15 and 18-23 respectively.

  1 Python 2.4.4 (#1, May 22 2007, 13:30:14)
  2 [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
  3 Type "help", "copyright", "credits" or "license" for more information.
  4  >>> import scriptutil as SU
  5  >>> flist = SU.ffindgrep('.',
  6  ...                       shellglobs=('README*', 'AUTH*'),
  7  ...                       namefs=(lambda s: '.svn' not in s,),
  8  ...                       regexl=('Django', 'doc'))
  9  >>> flist
 10 {'./django/contrib/redirects/README.TXT':
 11  '    * The file django/docs/redirects.txt in the Django distribution',
 12  './django/contrib/flatpages/README.TXT':
 13  '    * The file docs/flatpages.txt in the Django distribution',
 14  './README':
 15  '    * First, read docs/install.txt for instructions on installing Django.'}
 16 
 17  >>> SU.printr(flist)
 18     * First, read docs/install.txt for instructions on installing Django.
 19 ./README
 20     * The file docs/flatpages.txt in the Django distribution
 21 ./django/contrib/flatpages/README.TXT
 22     * The file django/docs/redirects.txt in the Django distribution
 23 ./django/contrib/redirects/README.TXT

Now I am passing an additional parameter (namely linenums) to the scriptutil.ffindgrep() function (line 28).

The added parameter (unsurprisingly) turns on the line numbers as can be seen on lines 30-35 and 37-42 respectively.

 24  >>> flist = SU.ffindgrep('.',
 25  ...                       shellglobs=('README*', 'AUTH*'),
 26  ...                       namefs=(lambda s: '.svn' not in s,),
 27  ...                       regexl=('Django', 'doc'),
 28  ...                       linenums=True)
 29  >>> flist
 30 {'./django/contrib/redirects/README.TXT':
 31  '5:    * The file django/docs/redirects.txt in the Django distribution',
 32  './django/contrib/flatpages/README.TXT':
 33  '5:    * The file docs/flatpages.txt in the Django distribution',
 34  './README':
 35  '8:    * First, read docs/install.txt for instructions on installing Django.'}
 36  >>> SU.printr(flist)
 37 8:    * First, read docs/install.txt for instructions on installing Django.
 38 ./README
 39 5:    * The file docs/flatpages.txt in the Django distribution
 40 ./django/contrib/flatpages/README.TXT
 41 5:    * The file django/docs/redirects.txt in the Django distribution
 42 ./django/contrib/redirects/README.TXT

For more detail on the scriptutil.ffindgrep() function please see also an earlier article.

About these ads

2 thoughts on “Turn on line numbers while searching in files

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s