Home > php教程 > PHP开发 > body text

Powerful grep command

高洛峰
Release: 2016-12-13 14:46:29
Original
1980 people have browsed it

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


Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template