VMS-to-Unix Phrase Book
<=  Return           

2.2  Searching For Strings

Problem

You want to search one or more files for a particular character string.

Solution

Use the fgrep which is the Unix program most analogous to the VMS search utility.

Discussion

The Unix grep (Globally search for Regular Expression and Print) utility is a very powerful program. Just a quick look at the man page will convince the casual observer that its learning curve is fairly steep. But most of the time, all you need is a straight forward sting match, much like VMS's more basic search utility.

The fgrep (fast grep) utility is a bit of a misnomer since it does not understand regular expressions, and it is not necessarily faster than grep. In fact, while in the past grep and fgrep were once two separate programs, now on most Unix systems fgrep is just an alias for the command:

    grep -F
Still, for the VMS user new to Unix, fgrep provides an easy to use transitional tool before venturing into the power and features of full-blown grep precisely because it does not use regular expressions. Regular expressions include the use of punctuation characters such as the "^" (up-arrow) to match the beginning of the line, the "$" (dollar sign) to match the end of the line, and the "." (period) to match any single character. In fact most punctuation characters are used as regular expression meta characters. Thus to use grep to search for the literal string
    $myVar
you need to remember to enter
    '\$myVar'
instead -- i.e. use the "\" (backslash) to turn off the special meaning of that character. And of course, as discussed in 1.5, the shell itself will act upon its own special characters. For this reason we recommend using 'strong quotes' when entering search strings unless you have a specific reason not to. Doing this, and using fgrep rather than grep, should give you a quick start at using a more familiar search tool and save you from having to wade into the mysteries of regular expressions until you are ready.

The order of the parameters for (f)grep are reversed from VMS the search command.

    search files string fgrep string files
This is because of a basic difference in how command line arguments are normally be grouped in each respective interface. The VMS syntax for search is designed to mirror English usage, i.e. "search these files for this string". There are exactly three parameters to the VMS search command. In DCL, multiple terms can be grouped together using a "," (comma).
    search this.file, that.file, another.file myvar
DCL groups the three filenames together as a single argument before starting the program. Unix shells do not support this type of syntax. Further more, in DCL, the command:
    search *.for myvar
has exactly three parameters even though the filespec *.for may match one, five, or a hundred files. The literal filespec *.for is passed verbatim to the program which, when launched, in turn uses a system call to expand the ambigous filespec. In contrast, when the shell is given the command:
    fgrep myvar *.f
it first expands the ambigous filespec *.f into all matching filespecs, and then launches the program. Thus the search string appears first since the assumption is that all of the following parameters will be file specifications.

Another important difference between DCL and Unix shells is in the area of character case. DCL, by default, maps the entire command line (excepted for "quoted strings") to uppercase before starting the command. This means that while you may have typed:

    search *.f myvar
what the SEARCH command sees for the string value is MYVAR. For this reason, and presumably as a matter of convenience, search is case insensitive, whereas (f)grep, like all of Unix, is case sensitive by default. Of course with both programs, quoting is required if the search string is to contain spaces.

With these differences in mind, consider the following phrase translations.

VMS UNIX Action
search file str
fgrep -i 'str' file
Do a case blind match.
search/exact file "str"
fgrep 'str' file
Do a case sensitive match.
search/numbers file str
fgrep -n 'str' file
Show line numbers on matched lines.
search/window=0 files str
fgrep -l 'str' files
List only the name of files containing string.
search/match=nor file str
fgrep -v 'str' file
Show lines that do not contain string.
search/match=nor/window=0 files str
fgrep -lv 'str' file
List names of files that do not contain string.

See Also

1.5 - Quoting;
Chapter 8 of Unix for OpenVMS Users ;
Chapter 27/28 of Unix Power Tools .


<=  Return           

Colophon:
Best Viewed With Any Browser
This page maintained by:
    Bill.Costa@unh.edu
    of the Enterprise Computing Group
    in the dept of Computing & Information Sevices
    at the University of New Hampshire

Typographical
Conventions

Created:  31-Jan-2001 BC
Revised:  27-Mar-2001 BC