Return the lines from a text block that match a regular expression.








regular- expression

character sequence that defines the pattern that the grep function searches for in text The regular-expression conforms to the regular expressions defined in the Unix ed(1) command and the Unixregexp(5) description. Following is a brief summary of several regular expression characters: 
^ beginning of line 
$ end of line 
. match any single character 
* match zero or more repetitions of the preceding 
[] match any of the characters contained within 
[^] match any characters except those contained within 
\< match any characters at the beginning of a word 
\> match any characters at the end of a word 
Word boundaries are delimited by any characters other than the following:

  • A through Z
  • a through z
  • 0 through 9
  • _


text that is searched for matches to regular-expression The text variable can be a text string enclosed in double quotation marks or one or more PSL commands that produce text as output.


flag that controls some grep() function options*Valid Values* 
v reverses the output of the grep() function, causing the grep() function to output all lines in text that do not contain a match for regular-expression . This flag is similar to the Unix grep -v flag. 
t ignore the meaning of special characters in regular-expression 
i ignore case when searching for regular-expression matches 
b stop searching for regular-expression matches after the first regular-expression match is found 
c returns the count of lines that match the regular expression in text 
n adds the line number to the front of each line returned by this function


The grep() function returns a list of the lines in text that match regular-expression. The t and v flags may be used either separately or together and may appear in either order.

The grep() function does not perform file I/O. You must use the cat() function for file I/O. For more information, see cat().


# search for "martin" substring in /etc/passwd
all_lines = cat("/etc/passwd"); # fill a buffer with passwd
matching_lines = grep("martin",all_lines);
# search for csh users in /etc/passwd
passwd_file_text = cat("/etc/passwd");
csh_users = grep("/bin/csh",passwd_file_text);
non_csh_users = grep("/bin/csh",passwd_file_text,"v");
# search for users who can’t log in (* in the password entry)
no_login = grep("*",passwd_file_text,"t"); # or...
no_login = grep("\\*",passwd_file_text); # escape the *
# search for users who can log in
login = grep("*",passwd_file_text,"vt"); # or...
login = grep("\\*",passwd_file_text,"v");
# search for "United" in the Declaration of Independence
all_lines = cat("E:\\Not\\Books\\US_Dec.txt");
matching_lines = grep("United",all_lines,"n");
print(matching_lines );

Output from preceding example:

1 The Declaration of Independence of The United States of America
150 We, therefore, the Representatives of the United States of America,
154 solemnly publish and declare, That these United Colonies are,

Was this page helpful? Yes No Submitting... Thank you