


In-depth understanding of grep command: application of regular expressions in grep
Introduction | How do I use the regular expressions of the Grep command in Linux and Unix-like systems? Linux comes with the GNU grep command tool, which supports extended regular expressions, and GNU grep is included by default in all Linux systems. The Grep command is used to search and locate any information stored on your server or workstation. |
Regular expression is a pattern used to match each line of input. The pattern refers to a sequence of characters. The following is an example:
^w1 w1|w2 [^ ]
Search for 'vivek'
in the /etc/passswd directorygrep vivek /etc/passwd
Output example:
vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bash vivekgite:x:1001:1001::/home/vivekgite:/bin/sh gitevivek:x:1002:1002::/home/gitevivek:/bin/sh
Search for vivek in any case (i.e. case-insensitive search)
grep -i -w vivek /etc/passwd
Search for vivek or raj in any case
grep -E -i -w 'vivek|raj' /etc/passwd
The last example above shows an extended regular expression pattern.
AnchorYou can use the ^ and $ symbols respectively to regularly match the beginning or end of the input line. The following example search displays only input lines starting with vivek:
grep ^vivek /etc/passwd
Output example:
vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bash vivekgite:x:1001:1001::/home/vivekgite:/bin/sh
You can only search for lines starting with the word vivek, that is, do not display vivekgit, vivekg, etc. (LCTT translation annotation: the word is followed by English word separators such as spaces and symbols.)
grep -w ^vivek /etc/passwd
Find lines ending with the word word:
grep 'foo$' 文件名
Matches only lines containing foo:
grep '^foo$' 文件名
The example shown below can search for empty lines:
grep '^$' 文件名
Match Vivek or vivek:
grep '[vV]ivek' 文件名
or
grep '[vV][iI][Vv][Ee][kK]' 文件名
Can also match numbers (i.e. match vivek1 or Vivek2, etc.):
grep -w '[vV]ivek[0-9]' 文件名
Can match two numeric characters (i.e. foo11, foo12, etc.):
grep 'foo[0-9][0-9]' 文件名
is not limited to numbers, but can also match at least one letter:
grep '[A-Za-z]' 文件名
Display all lines containing "w" or "n" characters:
grep [wn] 文件名
The expression placed in brackets, that is, the name of the character class enclosed between "[:" and ":]", represents a list of all characters belonging to this class. The standard character class names are as follows:
[:alnum:]
- Alphanumeric characters
[:alpha:]
- Alphabetic characters
[:blank:]
- Null characters: space and tab characters
[:digit:]
-Number: '0 1 2 3 4 5 6 7 8 9'
[:lower:]
- Lowercase letters: 'a b c d e f g h i j k l m n o p q r s t u v w x y z'
[:space:]
- Space characters: tab, line feed, vertical tab, form feed, carriage return and space character
[:upper:]
- Capital letters: 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'
In this example shown is matching all uppercase letters:
grep '[:upper:]' 文件名
You can use "." to match a single character. The example matches 3-character words starting with "b" and ending with "t":
grep '/<b.t/>' 文件名
here,
Match the empty string before the word
Matches the empty string after the word
Print out all lines with only two characters:
grep '^..$' 文件名
Display lines starting with a dot and a number:
grep '^/.[0-9]' 文件名
The following regular expression to match the IP address 192.168.1.254 is incorrect: (LCTT Annotation: It can match the IP address, but it is also possible to match a similar format in which the separator symbol is not a dot)
grep '192.168.1.254' /etc/hosts
All three dot characters need to be escaped:
grep '192/.168/.1/.254' /etc/hosts
The following example can only match the IP address: (LCTT Translation: In fact, due to the range of numbers in the IP address, this regular expression is not accurate)
egrep '[[:digit:]]{1,3}/.[[:digit:]]{1,3}/.[[:digit:]]{1,3}/.[[:digit:]]{1,3}' 文件名
Use the -e option to search for a string matching '--test--'. If you do not use the -e option, the grep command will try to parse '--test--' as its own option parameter:
grep -e '--test--' 文件名
Use the following syntax:
grep -E 'word1|word2' 文件名 或 egrep 'word1|word2' 文件名
or it could be
grep 'word1/|word2' 文件名
Use the following syntax to display all lines that contain both 'word1' and 'word2'
grep 'word1' 文件名 | grep 'word2'
Using the following syntax, you can detect the number of times a character appears repeatedly in a sequence:
{N} {N,} {min,max}
To match the character "v" appearing twice:
egrep "v{2}" 文件名
The following command can match "col" and "cool":
egrep 'co{1,2}l' 文件名
The following command will match all lines with at least three 'c' characters.
egrep 'c{3,}' 文件名
The following example will match mobile phone numbers in the format 91-1234567890 (that is, two digits-ten digits).
grep "[[:digit:]]/{2/}[ -]/?[[:digit:]]/{10/}" 文件名
Use the following syntax:
grep --color 正则表达式 文件名
使用如下语法:
grep -o 正则表达式 文件名
限定符 | 描述 |
---|---|
. | 匹配任意的一个字符。 |
? | 匹配前面的子表达式,最多一次。 |
* | 匹配前面的子表达式零次或多次。 |
+ | 匹配前面的子表达式一次或多次。 |
{N} | 匹配前面的子表达式 N 次。 |
{N,} | 匹配前面的子表达式 N 次到多次。 |
{N,M} | 匹配前面的子表达式 N 到 M 次,至少 N 次至多 M 次。 |
- | 只要不是在序列开始、结尾或者序列的结束点上,表示序列范围。 |
^ | 匹配一行开始的空字符串;也表示字符不在要匹配的列表中。 |
$ | 匹配一行末尾的空字符串。 |
\b | 匹配一个单词前后的空字符串。 |
\B | 匹配一个单词中间的空字符串。 |
\< | 匹配单词前面的空字符串。 |
\> | 匹配单词后面的空字符串。 |
egrep 等同于
grep -E
它会以扩展的正则表达式的模式来解释模式。下面来自 grep 的帮助页:
基本的正则表达式元字符 ?、+、 {、 |、 ( 和 ) 已经失去了它们原来的意义,要使用的话用反斜线的版本 /?、/+、/{、/|、/( 和 /) 来代替。 传统的 egrep 并不支持 { 元字符,一些 egrep 的实现是以 /{ 替代的,所以一个可移植的脚本应该避免在 grep -E 使用 { 符号,要匹配字面的 { 应该使用 [}]。
GNU grep -E 试图支持传统的用法,如果 { 出在在无效的间隔规范字符串这前,它就会假定 { 不是特殊字符。
例如,grep -E '{1' 命令搜索包含 {1 两个字符的串,而不会报出正则表达式语法错误。
POSIX.2 标准允许这种操作的扩展,但在可移植脚本文件里应该避免这样使用。
The above is the detailed content of In-depth understanding of grep command: application of regular expressions in grep. For more information, please follow other related articles on the PHP Chinese website!

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



The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

CentOS has been discontinued, alternatives include: 1. Rocky Linux (best compatibility); 2. AlmaLinux (compatible with CentOS); 3. Ubuntu Server (configuration required); 4. Red Hat Enterprise Linux (commercial version, paid license); 5. Oracle Linux (compatible with CentOS and RHEL). When migrating, considerations are: compatibility, availability, support, cost, and community support.

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.
