Grep regular expressions, options, and notes

Source: Internet
Author: User

Note: Some modifications have been made on the basis of the original article.

Grep command introduction:

In the ex Editor (I have never used it), when you start the ex editor and want to find a string, type:

:/Pattern/P
:/G/pattern/P

This is why grep is called. P indicates print, and when G appears before pattern, it indicates "all rows in the file" or "perform global replacement ".

The pattern to be searched is called a regular expression (Regular Expression). Therefore, replace pattern with RE, and then it becomes g/RE/P, grep.

Grep command syntax:

The origin of the previous name clearly tells us that grep is used to find the delimiter mode in one or more files. Both egrep and fgrep are only variants of grep. We will not introduce them here. Let's take a look at the syntax structure of grep.

# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>
# Include <sys/Wait. H>
Int main (INT argc, char * argv [])
{
Int PFD [2];
Pid_t CPID;
Char Buf;
If (argc! = 2)
{
Fprintf (stderr, "Usage: % S <string> \ n", argv [0]);
Exit (0 );
}
If (pipe (PFD) =-1)
{
Perror ("pipe ");
Exit (exit_failure );
}
CPID = fork ();
If (CPID =-1)
{
Perror ("fork ");
Exit (exit_failure );
}
If (CPID = 0)
{
Close (PFD [1]);/* close unused write end */
While (read (PFD [0], & Buf, 1)> 0)
Write (stdout_fileno, & Buf, 1 );
Write (stdout_fileno, "\ n", 1 );
Close (PFD [0]);
Exit (exit_success );
}
Else
{
Close (PFD [0]);/* close unused read end */
Write (PFD [1], argv [1], strlen (argv [1]);
Close (PFD [1]);/* reader will see EOF */
Wait (null);/* Wait for child */
Exit (exit_success );
}
}
# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>
# Include <sys/Wait. H>
Int main (INT argc, char * argv [])
{
Int PFD [2];
Pid_t CPID;
Char Buf;
If (argc! = 2)
{
Fprintf (stderr, "Usage: % S <string> \ n", argv [0]);
Exit (0 );
}
If (pipe (PFD) =-1)
{
Perror ("pipe ");
Exit (exit_failure );
}
CPID = fork ();
If (CPID =-1)
{
Perror ("fork ");
Exit (exit_failure );
}
If (CPID = 0)
{
Close (PFD [1]);/* close unused write end */
While (read (PFD [0], & Buf, 1)> 0)
Write (stdout_fileno, & Buf, 1 );
Write (stdout_fileno, "\ n", 1 );
Close (PFD [0]);
Exit (exit_success );
}
Else
{
Close (PFD [0]);/* close unused read end */
Write (PFD [1], argv [1], strlen (argv [1]);
Close (PFD [1]);/* reader will see EOF */
Wait (null);/* Wait for child */
Exit (exit_success );
}
}

The regular expression metacharacters used by grep:

Metacharacters in regular expressions are described in detail in Javascript logs. Here we will not explain what a regular expression is. Let's just look at the regular expression metacharacters used in grep.

Metacharacters Function Example Matching object
^ First line Separator '^ Simaopig %' Match All rows starting with simaopig
$ Line tail Locator 'Simaopig $' Match All rows ending with simaopig
. Match any character 'S. M' Matching contains one s character, followed by one character (random), followed by another M line
* Match 0 or multiple previous characters 'S * m' Matches zero or multiple S characters, followed by a line with M characters
[] Matches any one of a group of characters. '[Ss] imaopig' Match simaopig or simaopig
[^] Match characters not in the specified character group '[^ A-Z] imaopig' Match the line that does not contain characters between a-Z followed by imaopig, that is, all aimaopig-zimaopig rows do not contain (a bit around)
\ < First-word Locator '\ <Simaopig' Match the line of a word starting with simaopig. simaopigabcd is also acceptable.
\> Suffix 'Simaopig \>' Abcdsimaopig can also match the rows of words ending with simaopig.
\(..\) Mark matched characters '\ (Simaopig \)' s blog' Mark a character in a register, which is recorded as register 1. You can use \ 1 to repeat this mode When referencing this character later. The leftmost part of the nine tags is the first one. For example, the mode simaopig is saved in register 1 and then referenced by \ 1.
X \ {M \}, X \ {M, \}, or X \ {M, N \} Repeated occurrence of character x 'S \ {5 \} ','s \ {5, \}','s \ {5, 10 \}' Match rows with 5 S, at least 5 s, or 5 to 10 s consecutively

Grep options:

While viewing its syntax structure, grep has many options. In the following table, I will introduce the frequently used options.

Option Function
-B Add the block number in front of each row, which may be used to locate disk blocks Based on the context.
-C Displays the number of matched rows instead of the contents of the rows.
-H Do not display file name
-I Case Insensitive when comparing characters
-L (lowercase letter L) Only names of files with matching rows are listed (each file name is only listed once). Names are separated by line breaks.
-N Add the relative row number in the file before each row
-S Silent operation: displays only the error message, used to check the exit status
-V Reverse lookup: Only unmatched rows are displayed.
-W Use an expression as a word to search for it, as if it was clipped by \ <and \>. Applicable only to grep (not all grep versions support this function, for example, sco unix does not support it)

Grep simple example:

For example, in the first table, how do I find all rows with IF and display row numbers?

Grep-N if a.html

Output:

The most frequently used parameter is-V, but it is not easy to use.

For example, I want to find a word "userservice", but files like "*. SVN" do not need to be displayed. What should I do?

Grep-R "userservice"./| grep-V "SVN"

However, if I do not display files such as "test, auto_load", what should I do? My previous practices are:

Grep-R "userservice"./| grep-V "SVN" | grep-V "test" | grep-V "auto_load"

The command is very long and troublesome, so I thought that grep itself serves as an option based on a regular expression. Can I use the "or |" command of a regular expression?

Grep-R "userservice"./| grep-V "SVN | test | auto_load"

Obviously, the execution result shows that the above command does not meet my needs, so I am puzzled. It turns out that when you use the regular expression option, remember to escape "|. The final command is as follows:

Grep-R "userservice"./| grep-V "SVN \ | prj \ | test \ | auto_load"

Statement: This article uses BY-NC-SA protocol for authorization |
Reprinted from grep Regular Expressions and options and grep Regular Expression options remember to escape

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.