Use of the grep command in shell programming and the grep command

Source: Internet
Author: User

Use of the grep command in shell programming and the grep command

Before learning regular expressions, you must first make it clear and keep it in mind:

In linux, wildcards are interpreted by shell, while regular expressions are interpreted by commands. Do not confuse them. Remember !!!

Usually there areThree Text processing tools/commands: grep, sed, and awk. They can all interpret regular expressions. The following describes how to use the grep command.

 

1. Use the grep command to search for the content in the file

The grep and egrep commands can search for a specific character pattern in the content of one or more files, also known as regular expressions ).A mode can be a single character, a string, a word, or a sentence.

A regular expression is a pattern that describes a set of strings. A regular expression is a mathematical expression that combines a small expression into a new one by using an operator. A regular expression can be either plain text or special characters used to generate a pattern. To further define a search mode, the grep command supports metacharacters of the following regular expressions, also known as wildcards.

Ps: The Regular Expression Language consists of two basic character types: literal (normal) text characters and metacharacters. Metacharacters enable the regular expression to process. Metacharacters are special characters that have special meanings in regular expressions. They can be used to specify the mode in which the leading character (that is, the character located before the metacharacters) appears in the target object.

C *: matches 0 or more characters.

.: Match any one and only one character (except line breaks)

[Xyz]: match any character in square brackets

[^ Xyz]: match all characters that do not include the characters in square brackets

^: Start of the locked row

$: End of the locked row

In basic regular expressions, such as metacharacters *, +, {}, |, and () have lost their original meaning, if you want to reply to their original meaning, you should prefix the backslash \, such as \ * And \ +.

The grep command is used to search for a specific pattern in each file or standard output. When the grep command is used, each line containing a specified character pattern is printed on the screen. The syntax format of the grep command is as follows:

Grep option mode file name

The options can change the search method of the grep command. Except for the-w option, each of the other options can be used in the egrep and fgrep commands. Common grep commands are as follows:

-C (count): only lists the number of rows in the contain mode.

-I (ignore): uppercase and lowercase letters in ignore Mode

-L (list): list file names with matched rows

-N: list the row numbers before matching rows.

-V: List rows with no matching mode (reverse)

-W: The expression is used as a complete word to search, ignoring the partially matched rows.

-Q: silent mode, no output, $? To determine whether the execution is successful, that is, whether the desired content is filtered.

-A n: If the match is successful, the matching rows and the last n rows are printed together.

-B n: If the match is successful, the matching rows and the first n rows are printed together.

-C n: If the match is successful, the matching rows and the n rows before and after them are printed together.

Ps: if multiple files are searched, the result of the grep command is only the file name with the matching mode found in the file;

If you search for a single file, the result of the grep command displays each line containing the matching mode.

 

The above is the basic usage of the grep command. However, sometimes a simple regular expression cannot locate the content you want to search for. For example, to search for data rows that meet both mode 1 and Mode 2, The egrep command can be used in this case.

The syntax format of the egrep command is the same as that of the grep command. However, the egrep command is used to search for a specific mode by using the meta character of an extended regular expression in the content of one or more files. The metacharacters of the extended regular expression include the metacharacters of the regular expression used in the grep command, and some additional metacharacters are added, which are roughly as follows:

+: Match one or more leading characters

A | B: Match a or B

(RE): match the regular expression RE in parentheses

 

Below I will use some basic examples to enhance your understanding of grep and egrep command usage. Since all the options in the grep command can be used (egrep is more powerful), I will use the egrep command for your convenience:

Example 1: the/etc/passwd file contains the root row.

 

Example 2: output/etc/passwd any rows that contain bash, and output the content of the upper and lower rows that follow this line.

 

Example 3: How many rows in/etc/passwd contain nologin

 

Example 4: show the rows in/etc/passwd that contain root, and output the row numbers one by one.

 

Example 5:

Create a user, such as abominable, abominate, anomie, and atomize, and write regular expressions to match them.

 

 

Example 6: Create a new user alex127sb, Wpq2222b, yH438PIG, egon666, and egon to filter out a row consisting of letters, numbers, and letters.

 

Example 7: show all file names containing root under the/etc directory

 

Example 8: filter out all comments and empty lines in/etc/ssh/sshd_config.

 

The above example is only a small part. As long as you work diligently, you will soon be able to master the use of the grep command ^

 

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.