Detailed explanation of grep command and related examples
1. Match characters
. Match any single character
[ ] Match any character within the specified range
[^] Match any character within the specified range
[:alpha:] Alphabetic characters
[:lower :] Lowercase alphabetic characters
[:upper:] Uppercase alphabetic characters
[:digit:] Numbers
[:alnum:] Alphanumeric characters
[:space:] White space characters (printing is prohibited), such as carriage return characters, line feeds, vertical tabs and form feeds
[:punct:] punctuation characters
[:cntrl:] control characters (printing prohibited)
[:print:] printable characters
when used Generally, two square brackets are used, which will be used in the following examples.
2. Matching times
* Match the previous character any time
.* Match any character of any length (note the greedy mode, such as grep "r.*t" /etc/passwd )
x{m,n } Specifies that the preceding character appears at least m times and at most N times.
x{m,} Specifies that the previous character appears at least m times
x{0,n} Specifies that the previous character appears at most N times
x{m} Exactly matches m times
? Matches the preceding character 0 Or 1 time
Three. Anchor character
1.^ Anchor at the beginning of the line grep "^r..t" /etc/passwd
2.$ Anchor at the end of the line grep "h$" /etc/passwd
3.^$ Anchor blank line grep "^$" /etc/passwd
4.< (b) Anchor word beginning grep " 5.> (b) Anchor word beginning grep "r..t>" /etc/passwd Example (easy to confuse): contains at least one whitespace character grep "[[:space:]]{1,}" /etc /passwd contains at least one non-whitespace character grep "[^[:space:]]{1,}" /etc/passwd does not have a whitespace character grep -v "[^[:space:]]{1, }" /etc/passwd 6.() Group characters grep "(l..e).*1r" Example: grep --color "l([13]):1:.*: 1" /etc/inittab Four. Options -v Invert the result -i Ignore the case of letters -o Only display the matched string (other contents of the line are not displayed) -E Support extended regular expressions -A n Display n lines below the matched line -B n Display n lines above the matched line -C n Display n lines above and below the matched line Exercise: 1. Find relevant information about user1 in the system. (Create user11, myuser1 in advance) (Error-prone) grep "user1" /etc/passwd All lines containing user1 grep " useradd -c "user1's uncle" /etc/passwd -c is a comment grep "^ 2. Find the system that starts with user followed by a number User related information. grep "^user[0-9]{1,}>" /etc/passwd 3. Analyze the characteristics of the following two lines of text in the /etc/inittab file, and write a pattern similar to the two lines that can be accurately found , requires that the numbers in each row must be the same. l1:1:wait:/etc/rc.d/rc 1 l3:3:wait:/etc/rc.d/rc 3 grep "l([13]):1:.*: .* 1" inittab Extension: Match all the above characteristics: grep "l([0-9]):1:.*:.* 1" inittab If it exceeds 10, you need to add a minimum match: grep "l( [0-9]{1,}):1:.*:.* 1" inittab 4. Display the lines starting with case-insensitive s in the /proc/meminfo file grep "^[sS] " /proc/meminfo 5. Display the lines ending with nologin in /etc/passwd grep "nologin$" /etc/passwd 6. Display the lines starting with # in /etc/inittab and followed by one or Lines with multiple whitespace characters, followed by any non-whitespace characters grep "^#[[:space:]]{1,}[^[:space:]]" /etc/inittab 7. Display /etc/inittab contains lines with a number between two colons grep ":[0-9]:" /etc/inittab 8. Display the /boot/grub/grub.conf file with one or more lines with blank characters grep ":[0-9]:" /etc/inittab 9. Displays lines in the /etc/inittab file that start with a number and end with a number that has the same starting number. grep "^([0-9]).*1$" /etc/inittab 10. Display non-blank lines in the /etc/inittab file grep -v "^$" /etc/inittab 11. Get the relevant IP address of the current network interface (excluding 127.0.0.1) ifconfig |grep "inet addr" |grep -v "127.0.0.1"| cut -d: -f2|cut -d" " - f1 ifconfig |grep -A 1 "eth" |grep -o "addr:[0-9.]{1,}"|cut -d: -f2 5. Extended regular expressions are different from regular expressions Where: () is replaced with () {} is replaced with {} + Number of matches, match the character before it one or more times | or Example: appears in the ifconfig result The number is an integer between 1-255 ifconfig|grep --color -E "<([1-9]|[1-9][0-9]|1[1-9][0-9 ]|2[0-5][0-5])>"

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use grep command for log analysis in Linux? Introduction: Logs are important records generated during system operation. For system operation, maintenance and troubleshooting, log analysis is an essential task. In the Linux operating system, the grep command is a powerful text search tool that is very suitable for log analysis. This article will introduce how to use the grep command commonly used for log analysis and provide specific code examples. 1. Introduction to grep command grep is a file in Linux system

This article mainly studies the related content of grep displaying several lines of information before and after under Linux, as follows. Grep under standard unix/linux controls the context through the following parameters. grep-C5foofile displays the line matching the foo string in the file file and the upper and lower 5 lines. grep-B5foofile displays foo and the first 5 lines. grep-A5foofile displays foo and the last 5 lines to view the grep version. The method of numbering is grep-V. If you want to upgrade, the upgrade method: the latest source code (google or Baidu search homepage), compile and install it somewhere, such as /home/aaa/bin/, then use / when using it in the future. home/aaa/

If you want to view the contents of a file in a LINUX system, how can you view the first and last contents of a specified file? Let's take a look at the tutorial on using grep to query the contents of the specified first and last files. 1. Open LINUX, here we can use the UBUNTU operating system. 2. Find TERMINAL on the left taskbar and open the terminal window. 3. Add ^ after grep to specify the beginning of a certain line. 4. Add $ after the content after grep to specify the end of a certain line. 5. ^$ can be used at the same time to specify the necessary content at the beginning and end of a certain line. 6. If a character in the middle is missing or incorrect, it will not be queried. 7. Use with -i to ignore the case rules. 8. You can also use -v

The Linuxgrep command is used to find strings that meet conditions in files. The grep command is used to find files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name is -, the grep command reads data from the standard input device. Syntax: grep parameters: -a or --text: Do not ignore binary data. -A or --after-context=: In addition to displaying the column that conforms to the template style, and display the content after the line. -b or --byte-offset: before displaying the line that matches the style

In Linux, grep is a very common and important tool. It is a command that every professional operation and maintenance engineer must master, because it can quickly find and filter the contents of files. So how to use grep in Linux system? The following is Let’s take a look at the common usage introduction. 1. Basic usage The grep command is mainly used to search for lines of a specified pattern in files. For example, to find the line containing "example" in the file file.txt, you can use the grep command. grep 'example' file.txt grep will output all lines containing 'example'.

The Linuxgrep command is used to find strings that meet conditions in files. The grep command is used to find files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name is -, the grep command reads data from the standard input device. Syntax: grep parameters: -a or --text: Do not ignore binary data. -A or --after-context=: In addition to displaying the column that conforms to the template style, and display the content after the line. -b or --byte-offset: before displaying the line that matches the style

The regular usage of grep includes: 1. Simple matching; 2. Basic regular expressions; 3. Use of metacharacters; 4. Use of anchor characters; 5. Use of character classes; 6. Use of quantifiers. Detailed introduction: 1. Simple matching, use the grep command followed by the string to be matched; 2. Basic regular expressions, use the -E option to enable extended regular expression functions; 3. The use of metacharacters, in regular expressions , you can use some metacharacters to represent specific characters or character sets; 4. The use of anchor characters, etc.

The usage of grep in Linux includes basic usage, ignoring case, regular expression search, reverse search, counting the number of matching lines, recursive search, output line number and searching from the input stream, etc. Detailed introduction: 1. Basic usage. The basic usage of the grep command is to find lines containing the specified pattern in the file. To find the lines containing "example" in the file file.txt, you can execute the command "grep "example" file.txt "; 2. Ignore case. By default, grep is case-sensitive and so on.
