Advertisements

An introduction to grep

Why do I need grep I hear you say,  I’ve got vCenter and wonderful Powershell for a guide in the dim depths of Console land, haven’t I?

Yes and no, some of the really good stuff can only be done from the command line.   So what exactly is grep?

So what is grep?  or to give it is full Sunday name the Global Regular Expression Print.

grep is a powerful ‘NIX based tool to search files for some phrase, or, as the name suggests, any expression. Using grep, you can discover exactly which file contains the username of idiot who just deleted your favourite VM.   Simply give grep an expression, a place to look, and it will return any matches, in context.

grep searches a input file for lines containing a match to a given pattern list.   When it finds a match in a line, it copies the line to standard output (by default), you can redirect the screen output into another file and then manipulate it.

Though grep expects to do the matching on text, it has no limits on input line length other than available memory, and it can match arbitrary characters within a line. If the final byte of an input file is not a newline, grep silently supplies one. Since newline is also a separator for the list of patterns, there is no way to match newline characters in a text.

So how to I use this mythical beast of the CLI?

well obviously you perform the incantation of invocation 😉 or in other words you type at the console

grep [options] expression [file(s)]

Options

as already mentioned grep is a very powerful tool and contains many options,  this can be expressed either as -c -e or –count or –pattern and can be used to build up quite complex functions.

what follows below is a outline of the main options flags used by grep.

`-c’ `–count’ Suppress normal output; instead print a count of matching lines for each input file. With the `-v’, `–invert-match’ option, count non-matching lines.

`-e pattern’ `–regexp=pattern’ Use pattern as the pattern; useful to protect patterns beginning with a `-‘.

`-f file’ `–file=file’ Obtain patterns from file, one per line. The empty file contains zero patterns, and therefore matches nothing.

`-i’ `–ignore-case’ Ignore case distinctions in both the pattern and the input files.

`-l’ `–files-with-matches’ Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning of every file will stop on the first match.

`-n’ `–line-number’ Prefix each line of output with the line number within its input file.

`-o’ `–only-matching’ Print only the part of matching lines that actually matches pattern.

`-q’ `–quiet’ `–silent’ Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the `-s’ or `–no-messages’ option.

`-s’ `–no-messages’ Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, traditional grep did not conform to POSIX.2, because traditional grep lacked a `-q’ option and its `-s’ option behaved like GNU grep’s `-q’ option. Shell scripts intended to be portable to traditional grep should avoid both `-q’ and `-s’ and should redirect output to `/dev/null’ instead.

`-v’ `–invert-match’ Invert the sense of matching, to select non-matching lines.

`-x’ `–line-regexp‘ Select only those matches that exactly match the whole line

Usage

As already mentioned the basic syntax of grep is as follows:

grep [options] expression [file(s)]

For example, if you wanted to find the word “nano” in your install.log in your root directory:

$ grep nano /root/install.log

Grep would then print on the screen a line similar to the following:

Installing nano-1.2.1-4.i386
...
.
.

“whoppee woo!  I can search for words more easily using my text editor!”

That’s true you can, but can you search an entire directory for that particular word?  Could you search your entire system? With grep, you can.

Using the wildcard symbol you can have grep search every file in the sound directory, however remember that grep is case sensitive by default, if you do not know the exact syntax of the expression being searched use the case insensitive option (-i), to see if might you have more success.

$ grep -i "my expression" myfolder/*

to search the entire file system recursively use the -r option, and issue the command:

$ grep -r "my phrase" /

This searches the entire system to find one short phrase.   A word of caution here, if you do try this, searching every file will take a long time, regardless of the speed of your server.  so once you see the file that you were looking for and grep continues to look for more, use ctrl+C to stop grep.

Now for an ESX focused example

however most of the time, you will use it to single out the line from a long output from some command so that you don’t have to wade through pages of technical info looking for the right data.

For example:

esxcfg-info -w | grep -i hv

The esxcfg-info command returns several pages of technical info from your setup.

By using grep you can single out the line with “HV” in there so you can see if 64 bits is supported in one glance and that all by just piping the output from the first command straight into grep.

Very convenient…

There are many more useful things you can do with grep than I can describe here. Hopefully, I have convinced you that it is a tool worth using. I suggest you take a look at the manual page, as well as other resources that might be available

Advertisements

2 comments

  1. I’m trying to get

    grep -f PatternFile.txt FileToSearch.txt

    working as expected. I expect it to OR the patterns from each line of PatternFile.txt and use that to search FileToSearch.txt

    Is that what it’s supposed to do?

    Any considerations (newlines, blank lines etc) that might make it not work on anything but the simplest examples?

  2. Hi,

    That is exactly how it works. However you have to be very careful of not adding a blank line at the end of your pattern file. If you made the mistake of doing that, then suddenly ALL of your text in FileToSearch.txt will match and it will just be displayed as if you used cat to print it all.


    Wil

Comments have been disabled.

%d bloggers like this: