Home php教程 PHP开发 Powerful grep command

Powerful grep command

Dec 13, 2016 pm 02:46 PM

There are a lot of explanations about the grep command on the Internet, and they are basically a rough translation of the man command. To be honest, I will forget it after reading it! Personally, I think the best way to learn commands is to simulate the real environment, write down some command combinations that may be used, and then slowly digest them. At least that's how I came here. At the beginning, it was basically rote memorization, and then I slowly expanded my horizons. As time went by, I accumulated more. Okay, let’s get to the point and talk about the awesome grep command.

grep is the abbreviation of general regular expression parser. We can simply understand it as a search command in the Linux system. Next, let’s go directly to the example:

1. Search for the specified string

In the /etc/passwd file, search for the string guolei:

grep 'guolei' /etc/passwd
Copy after login

Note that the quotation marks for guolei can be omitted. But if there are spaces in the search string or you use regular expressions, you need to add them.

2. Search for the specified string in multiple files

In the current directory, search for files containing the string guolei:

grep -r guolei *
Copy after login

Note: -r is the abbreviation of recursive, which means recursive search.

In the .java file in the current directory, search for files containing the string guolei:

grep -r guolei *.java
Copy after login

Sometimes, our search results may be more, we can combine the less command to display the results

grep -r guolei *.java | less
Copy after login

or there are more search results, We only need to list the file names:

grep -rl guolei *.java
Copy after login

There is another requirement that is more common. We often want to find files containing a specified string in a certain directory. Note that the above command cannot be searched recursively. For example, we want to recursively search for all .java files in the current directory that contain the string guolei:

find . -type f -name *.java -exec grep -il guolei {} \;
Copy after login

3. Ignore case when searching

When searching for guolei, ignore case:

grep -ri guolei *
Copy after login

Note :-i is the abbreviation of Ignore case, which means ignoring case.

4. List the line number in the search results

In the search results, list the line number where the string appears:

grep -rn guolei *.java
Copy after login

Note that -n is the abbreviation of number, which means the line number.

5. Reverse search

In actual development, there is another situation that is more common. We need to search for files that do not contain a certain string in a certain directory:

grep -riv guolei * | less
Copy after login

Note: -v is reverse The abbreviation of means reverse. The above example searches for files that do not contain guolei in the current directory.

6. Use grep in pipelines

We often also use grep in pipeline commands, this is the most common. For example, we want to search for the mysql process in the current system:

ps -ef | grep mysql
Copy after login

or list files ending with html in the current directory:

ls | grep 'html$'
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)