Sven Guckes ©1995-2001

Last update: Sat Nov 17 02:40:00 MET 2001

agrep - approximate grep tool

NAME agrep - search a file for a string or regular expression, with approximate matching capabilities SYNOPSIS agrep [-#cdehiklnpstvwxBDGIS] pattern [-f patternfile] [filename...] I mainly use agrep for one thing - extract signatures from my signature file.


AGREP and VIM - Adding signatures

You can easily add signatures from a signature collection file using agrep. The idea is to tell agrep to find some word between two copies of a special line - a "sigdashes line" (aka "sigdashes" for short), ie a line containing only dash-dash-space (no more, no less).

Here's how it works: Put all your signatures into one file, say, $HOME/.Signatures. [NOTE: *Dont* use the filename $HOME/.signature as this is the default filename that many programs use for the signature. This could simply lead to some confusion.] Separate signatures with "sigdashes" (you know, dash-dash-space ;-). Example:

-- 
Sven Guckes sig-mutt @guckes.net  using mutt-1.2.5 [000729]      ,,
MUTT http://www.mutt.org/               news:comp.mail.mutt   (__/'.
MUTT http://www.guckes.net/mutt/  ->     sample setup files   /| |\
MUTT MUTT - *the* mailer for UNIX with color, threading, IMAP+MIME+PGP+POP
-- 
Sven Guckes guckes@vim.org                    [vim-version]
VIM VERSION latest user release:        VIM-5.7    [000624]
VIM VERSION latest user release patch:  VIM-5.7.26 [010309]
VIM VERSION latest developer release:   VIM-6.0z   [010325]

And from within Vim you use the ":read" command to read in the putput of the "agrep" command. To understand how this works try the command :r!date which reads in the current date and time of your machine after the current line.

Let's try agrep on the shell first: Use option "-d" which tells agrep that the next "word" is the pattern that describes the "delimiter". Patterns (aka "regular expressions") are a poweful way of describing sets of strings with "meta characters".

A '^' ("caret") denotes the start-of-line, and a '$' ("dollar") denotes the end-of-line. Therefore a line that contains only dash-dash-space is described with ^-- $.

Now try this command on your ".Signatures" file:

$ agrep -d  "^-- $" muttsig $HOME/.Signatures
-- 
Sven Guckes sig-mutt @guckes.net  using mutt-1.2.5 [000729]      ,,
MUTT http://www.mutt.org/               news:comp.mail.mutt   (__/'.
MUTT http://www.guckes.net/mutt/  ->     sample setup files   /| |\
MUTT MUTT - *the* mailer for UNIX with color, threading, IMAP+MIME+PGP+POP

As you can see, agrep has now extracted the lines between two sigdashes lines which contains the word "muttsig". Please note that the first delimiter (sigdashes) are also in the output. No need to add sigdashes. :-)

Now all you need to know is that the Vi command :read can not only read in the contents of some file but also the output of a command:

   :read !agrep -d  "^-- $" keyword $HOME/.Signatures
Mind you, this is a standard Vi feature. No special improvement by Vim here.

So here it is: The mapping that will put this command onto your command line, including the cursor movements which palces the cursor right at the position to type in the keyword for your special signature:

" Attaching a signature at the end of the text map ,s :$r!agrep -d "^-- $" '' ~/.signatures <S-Left><S-Left><right> And if your want to test this on some signature file, well, here is mine:

  • http://www.guckes.net/sig/SIGS

    Enjoy! :-)


    Eliminating paragraphs

    The following example eliminates all paragraphs with a given keyword from the currently edit buffer in Vi (and clones): :%!agrep -v -d "^$" Windows This has improved many texts for me. :-)

            -v      print only non-matching items
            -d      next parameter gives delimiter
            ^$      an empty line (start followed directly by end)
            Windows to be eliminated!
    

    Links

    agrep page by Thomas Gries [010414,011117]
    http://www.tgries.de/agrep/
    A page on agrep with quite some info. I understand that Thomas Gries implemented agrep for DOS and OS/2. he intends to improve the win32 version, too.

    agrep main site [010414]
    ftp://ftp.cs.arizona.edu/agrep/
    Latest version: agrep 2.04 [920415]

    agrep source on ftp.fu-berlin.de
    http://ftp.fu-berlin.de/mirror/agrep.html
    ftp://ftp.fu-berlin.de/unix/tools/agrep/

    agrep-2.01 announcement [920223;010414]
    http://sources.isc.org/utils/text/searching/agrep-2.01.txt

    agrep 1.02 binaries for RiscOS [010414]
    http://www.mirror.ac.uk/collections/hensa-micros/local/riscos/textprocess/agrep.txt
    http://www.mirror.ac.uk/collections/hensa-micros/local/riscos/textprocess/agrep.arc

    agrep authors and BibTeX entry [010414]
    Sun Wu sw(at)cs.arizona.edu Udi Manber udi(at)cs.arizona.edu.
    S. Wu and U. Manber
    Agrep: A Fast Approximate Pattern-Matching Tool
    
    @inproceedings{Wu:91,
      AUTHOR = {S. Wu and U. Manber},
      TITLE = {Agrep: A Fast Approximate Pattern-Matching Tool},
      YEAR = 1991,
      BOOKTITLE = {Proc.\ of the Winter 1992 USENIX Conference},
      ADDRESS = {San Francisco, California},
      PAGES = {153-162},
      KEYWORDS = {}}
    


    URL:         http://www.guckes.net/agrep/
    Created:     Fri Jun 20 00:00:00 CEST 1997
    
    Send feedback on this page to
    Sven Guckes webpage-agrep@guckes.net