grep()
Return the lines from a text block that match a regular expression.
Syntax
grep(regular-expression,text,[vtcibn])
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
|
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.
The grep()
function does not perform file I/O. You must use the cat()
function for file I/O. For more information, see cat().
Example
# 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,
Comments
Log in or register to comment.