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

12 practical examples of grep command in Linux

高洛峰
Release: 2016-12-13 14:38:48
Original
1152 people have browsed it

grep is a powerful file pattern search tool that comes pre-installed with every Linux distribution. For whatever reason, if your system does not come pre-installed, you can easily install it through your system's package manager (apt-get on Debian/Ubuntu systems and yum on RHEl/CentOS/Fedora systems) .

$ sudo apt-get install grep #Debian/Ubuntu $ sudo yum install grep #RHEL/CentOS/Fedora

I find the easiest way to get your feet wet with the grep command is to use real world examples to get you involved. .

1. Search and find files

Suppose you have installed a fresh Ubuntu on your computer, and then you plan to uninstall Python. You browse the web looking for tutorials, but you find that there are two different versions of Python in use, and you don't know which version of Python your Ubuntu installer installed on your system, nor what modules it installed. To solve this problem, simply run the following command:

$ sudo dpkg -l | grep -i python

Output example

ii python2.7 2.7.3-0ubuntu3.4 Interactive high-level object-oriented language ( version 2.7)
ii python2.7-minimal 2.7.3-0ubuntu3.4 Minimal subset of the Python language (version 2.7)
ii python-openssl 0.12-1ubuntu2.1 Python wrapper around the OpenSSL library
ii python-pam 0.4. 2-12.2ubuntu4 A Python interface to the PAM library

First, we run dpkg -l to list the .deb packages installed on your system. Next, we use a pipeline to transfer the output results to the command grep -ipython. This step can be simply explained as transferring the results to grep and then filtering out all items containing python and returning the results. The -i option is used to ignore case, because grep is case-sensitive. It's a good idea to use option -i unless you plan to perform a more detailed search.

2. Search and filter files

grep can also be used to search and filter in one or more files. Let's look at a scenario like this:

There is a problem with your Apache web server, and you have to find one of many professional websites to post questions. The person who kindly replied to you asked you to paste the contents of your /etc/apache2/sites-available/default-ssl file. If you could remove all the comment lines, wouldn't it be easier to spot the problem for you, for the people helping you, and for everyone else who reads the file? Of course you can do it easily! Just do this:

$ sudo grep -v "#" /etc/apache2/sites-available/default-ssl

The -v option tells the grep command to reverse its output, which means not To print the matching items, do the opposite and print out all the non-matching items. In this example, the lines with # are comment lines (Annotation: In fact, this command is not accurate, and the lines containing "#" are not all comment lines. Regarding how to accurately match the comment lines, you can learn more about regular expressions. ).

3. Find all mp3 files

The grep command is very useful for filtering results from standard output. For example, suppose you have a folder full of music files in various formats. You need to find all the mp3 format music files of artist jayZ, and there should be no mixed audio tracks in them. This magic can be accomplished by using the find command combined with grep through pipes:

$ sudo find . -name ".mp3" | grep -i JayZ | grep -vi "remix""

In this example, we use find The command prints out all files with the .mp3 suffix, then pipes them to grep -i to filter and print out files named "JayZ", and then pipes them to grep -vi to filter out files containing "remix"

35 practical examples of find command in Linux

4. Display line numbers before or after the search string

The other two options are to switch between -A and -B, which are used to display matches The line and line number respectively control the number of lines displayed before or after the string. The Man page gives a more detailed explanation. I found a little trick to remember: -A=after, -B=before.

$ sudo ifconfig | grep -A 4 etho $ sudo ifconfig | grep -B 2 UP


5. Print line numbers around the matching string

The -C option of the grep command is very similar to that in Example 4, but prints It does not match the lines before or after the string, but prints out the lines matching in both directions (Annotation: Same as the memory trick above: -C=center, centered on this): $ sudo ifconfig | grep -C 2 lo

6. Count the number of matches

This function is similar to piping the output of grep to the counter (wc program). The built-in option of grep can achieve the same purpose:

$ sudo ifconfig | grep -c inet6


7. Search for matching line numbers in the file based on a given string

The -n option of the grep command is a very useful function when you need to debug when compilation errors occur. Which line of the file is the content you are searching for:

$ sudo grep -n "main" setup.py


8. Search recursively in all directories

If you want to search for a string in the current folder, and there are many subdirectories in the current folder, you can specify a -r option to facilitate recursive search: $ sudo grep -r "function" *

9 .Perform an exact match search

Pass the -w option to the grep command to perform an exact match search in a string (Annotation: contains the word to be searched, not a wildcard). For example, typing like this:

$ sudo ifconfig | grep -w “RUNNING”

will print out lines containing matches within quotes. In addition, you can also try this:

$ sudo ifconfig | grep -w "RUN"

When searching for this match, if there is no such a single word in the searched thing, nothing will be returned.

10. Search in Gzip compressed files

We also want to pay attention to the derivative applications of grep. The first is zgrep, which is very similar to zcat and can be used for gzip-compressed files. It has similar command options to grep and is used in the same way:

$ sudo zgrep -i error /var/log/syslog.2.gz

11. Match regular expressions in files

egrep is another Derived application, which stands for "extended global regular expressions". It recognizes more regular expression metacharacters, such as at + ? | and (). Egrep is a very useful tool when searching for source code files. There are also other search needs for fragmented code files, making such search capabilities necessary. It can be enabled using option -E in grep command.

$ sudo grep -E

12. Search for a fixed matching string

fgrep is used to search for a fixed pattern string in a file or file list. The function is the same as grep -F. A common use of fgrep is to pass a file containing styles to it:

$ sudo fgrep -f file_full_of_patterns.txt file_to_search.txt

This is just the beginning of the grep command. You may have noticed that it is useful for implementing various It is so useful for all kinds of needs. In addition to the one-line command we run, grep can also be written as a cron task or an automatic shell script for execution. Be curious, experiment with the options on the man page, and write some grep expressions for your purposes.


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!