|HP-UX Reference > G
HP-UX 11i Version 3: February 2007
grep, egrep, fgrep — search a file for a pattern
Call with (multiple) -e pattern
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern... [-e pattern] ... [file ...]
The grep command searches the input text files (standard input default) for lines matching a pattern. Normally, each line found is copied to the standard output. grep supports the Basic Regular Expression syntax (see regexp(5)). The -E option (egrep) supports Extended Regular Expression (ERE) syntax (see regexp(5)). The -F option (fgrep) searches for fixed strings using the fast Boyer-Moore string searching algorithm. The -E and -F options treat newlines embedded in the pattern as alternation characters. A null expression or string matches every line.
The forms egrep and fgrep are maintained for backward compatibility. The use of the -E and -F options is recommended for portability.
The file name is output in all the cases in which output is generated if there are more than one input file, unless the -h option is specified. Care should be taken when using the characters $, *, [, ^, |, (, ), and \ in expression, because they are also meaningful to the shell. It is safest to enclose the entire expression argument in single quotes ('...').
LANG determines the locale to use for the locale categories when both LC_ALL and the corresponding environment variable (beginning with LC_) do not specify a locale. If LANG is not specified or is set to the empty string, a default of C (see lang(5)) is used.
LC_ALL determines the locale to use to override any values for locale categories specified by the settings of LANG or any environment variables beginning with LC_.
LC_COLLATE determines the collating sequence used in evaluating regular expressions.
LC_CTYPE determines the interpretation of text as single byte and/or multi-byte characters, the classification of characters as letters, the case information for the -i option, and the characters matched by character class expressions in regular expressions.
LC_MESSAGES determines the language in which messages are displayed.
If any internationalization variable contains an invalid setting, the commands behave as if all internationalization variables are set to C. See environ(5).
Upon completion, grep returns one of the following values:
In the POSIX shell ( sh(1)) the following example searches two files, finding all lines containing occurrences of any of four strings:
grep -F 'if then else fi' file1 file2
Note that the single quotes are necessary to tell grep -F when the strings have ended and the file names have begun.
For the C shell (see csh(1)) the following command can be used:
grep -F 'if\ then\ else\ fi' file1 file2
To search a file named address containing the following entries:
Ken 112 Warring St. Apt. A Judy 387 Bowditch Apt. 12 Ann 429 Sixth St.
grep Judy address
Judy 387 Bowditch Apt. 12
To search a file for lines that contain either a Dec or Nov, use either of the following commands:
grep -E '[Dd]ec|[Nn]ov' file egrep -i 'dec|nov' file
Search all files in the current directory for the string xyz:
grep xyz *
Search all files in the current directory subtree for the string xyz, and ensure that no error occurs due to file name expansion exceeding system argument list limits:
find . -type f -print |xargs grep xyz
The previous example does not print the name of files where string xyz appears. To force grep to print file names, add a second argument to the grep command portion of the command line:
find . -type f -print |xargs grep xyz /dev/null
In this form, the first file name is that produced by find, and the second file name is the null file.
(XPG4 only.) If the -q option is specified, the exit status will be zero if an input line is selected, even if an error was detected. Otherwise, default actions will be performed.
If the -w option is specified with non-word constituent characters, then the output is unexpected.