grep()
Return the lines from a text block that match a regular expression.
Syntax
Parameter
Parameter | Definition |
---|---|
Parameter | Definition |
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:
|
text | 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. |
vtcibn | flag that controls some grep() function options*Valid Values* |
Description
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.
Example
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:
150 We, therefore, the Representatives of the United States of America,
154 solemnly publish and declare, That these United Colonies are,