12 practical examples of the grep command in Linux

Source: Internet
Author: User
Tags grep regular expression openssl library

Have you ever encountered the need to find a specific string or style in the file, but you do not know where to start? Then, ask grep to help you.

Grep is a powerful file mode search tool pre-installed in each Linux release. For whatever reason, if your system is not preinstalled with it, you can easily install it through the system Package Manager (apt-get In Debian/Ubuntu and yum in RHEl/CentOS/Fedora ).

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

I found that using real examples in the real world to get you involved is the easiest way to get in touch with the grep command.

1. Search for and find files. Suppose you have installed a brand new Ubuntu on your computer, and you plan to uninstall Python. You can browse the Web page to find a tutorial, but you find that two different versions of Python are in use, and you don't know which version of Python is installed in your Ubuntu installer, I do not know which modules it has 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, run dpkg-l to list the. deb packages installed on your system. Next, we use the pipeline to transmit the output results to the command grep-I python. This step can be simply interpreted as transmitting the results to grep, then filtering out all the items containing python and returning the results. -I option is used to ignore case sensitivity because grep is case sensitive. Use Option-I is a good habit Unless you plan to perform a more detailed search.

2. You can search and filter the grep file in one or more files. Let's look at a scenario like this:

If your Apache web server has a problem, you have to ask for a post from many professional websites. Kindly reply to your recipients and ask you to paste your/etc/apache2/sites-available/default-ssl file content. If you can remove all comments, isn't it easier for you, the people who help you, and all who read the file to find problems? Of course you can do it easily! You only need to do this:

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

Option-v tells the grep command to reverse the output result, which means that the matching items are not output, and the opposite is done to print out all mismatched items. In this example, there is a # comment line (in fact, this command is not accurate, the lines containing "#" are not all comment lines. For more information about how to precisely match a comment row, see the regular expression .).

3. Finding out all the mp3 file grep commands is very useful for filtering results from standard output. For example, assume that a folder contains all music files of various formats. You need to find all mp3 music files from artist jayZ without any mixed audio tracks. Use the find command and the pipeline to use grep to complete this magic:

$ Sudo find.-name ". mp3" | grep-I JayZ | grep-vi "remix ""

In this example, we use the find command to print all files suffixed with suffix, and then pass them to grep-I to filter and print the files named "JayZ, then, use the pipeline to send to grep-vi to filter out items containing "remix.

4. display the row number before or after the search string. The other two options are-A and-B. They are used to display the matched row and row number, control the number of lines displayed before or after a string. The Man page provides A more detailed explanation. I found A memory tips:-A = after,-B = before.

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

5. the-C option for printing the travel number grep command around the matching string is similar to that in example 4, but it does not print the line before or after the matching string, instead, print the rows that match both directions (similar to the preceding example:-C = center, with this as the center ):

$ Sudo ifconfig | grep-C 2 lo

6. Calculate the number of matching items. This function is similar to transferring the grep output result to the counter (wc program) using pipelines. The grep built-in options can achieve the same purpose:

$ Sudo ifconfig | grep-c inet6

For more details, please continue to read the highlights on the next page:

Grep uses concise and Regular Expressions

Linux basic commands-grep

Grep Regular Expression learning notes

Linux grep commands and Regular Expressions

Shell programming in Linux -- basic use of the grep command

Powerful search command grep in Linux

  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.