You're not using regular? See 12 examples of GREP commands in Linux

Source: Internet
Author: User
Tags openssl library egrep

Have you ever encountered a need to find a specific string or style in a file, but don't know where to start? So, just ask grep to help you.


grep is a powerful file-mode search tool preinstalled on every Linux distribution. Whatever the reason, if your system is not preloaded with it, you can easily install it through the system's Package Manager (Apt-get in the Debian/ubuntu and Yum in the Rhel/centos/fedora department).


$ sudo apt-get install grep #Debian/ubuntu $ sudo yum install grep #RHEL/centos/fedora


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


1. Search and find Files


Suppose you already have a brand new Ubuntu installed on your computer, and then you're going to uninstall Python. You browse the Web for tutorials, but you find that there are two different versions of Python in use, and you don't know exactly which version of Python your Ubuntu installer has installed on your system or what modules it installs. To solve this problem, simply run the following command:


$ sudo dpkg-l | Grep-i python


Output example


   IIPython2.7         2.7.3-0ubuntu3.4 Interactive High- LevelObject-oriented language (version 2.7)

   IIPython2.7-Minimal 2.7.3-0ubuntu3.4 Minimal subset of the Python language (version 2.7)

   IIpython-OpenSSL    0.12-1ubuntu2.1  Python wrapper around the OpenSSL library

IIpython-Pam        0.4.2-12.2ubuntu4 A PythonInterface to The PAMLibrary


First, we run dpkg-l to list the. deb packages installed on your system. We then use the pipeline to transmit the output to the command Grep-i python, which can be simply explained by transferring the results to grep and then filtering out all the items that contain Python and returning the results. The –i option is used to ignore case because grep is case-sensitive. Using option-I is a good habit, unless you intend to do a more detailed search.


2. Search and Filter files


Grep can also be used for searching and filtering 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 ask for a post from many professional websites. Kindly reply your person to ask you to paste up your/etc/apache2/sites-available/default-ssl file content. If you could remove all the comment lines, wouldn't it be easier for you, for the people who helped you, and for all the people who read the file, to find the problem? Of course you can do it easily! This is all you need to do:


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


The option-V tells the grep command to reverse its output, meaning that it does not output matching items and does the opposite, printing out all the mismatched items. In this example, there is a comment line (note: In fact, this command is not accurate, the line containing "#" is not all comment lines.) You can learn more about regular expressions by precisely matching comment lines. )。


3. Find all the MP3 files


The grep command is useful for filtering results from standard output. For example, suppose you have a folder full of music files in a variety of formats. You need to find out all the music files in the MP3 format of artist JayZ, and don't have any mixed audio tracks inside. Using grep with the Find command to combine pipelines can accomplish this magic:


$ sudo find. -name ". mp3" | Grep-i JayZ | Grep-vi "Remix" "


In this example, we use the Find command to print out all files with the suffix. mp3, and then pass it to Grep-i to filter and print out a file named "JayZ", and then use the pipe to send to Grep-vi to filter out items that contain "remix".


4. Display line numbers before or after the search string


The other two options are toggle between A and-B, which is used to display matching rows and line numbers, respectively, to control the number of lines displayed before or after the string. Man page gives a more detailed explanation, I found a memory of the tips:-a=after,-b=before.


$ sudo ifconfig | GREP-A 4 etho $ sudo ifconfig | Grep-b 2 Up


5. Print a travel number around the matching string


The-c option of the grep command is similar to that in Example 4, but it does not print the lines that match the string's front or back, but instead prints the rows that match the two directions (the same as the memory trick above:-c=center, centered): $ sudo ifconfig | Grep-c 2 Lo


6. Calculate the number of matches


This function is similar to the result of grep output piped to the counter (WC program), and grep built-in options can achieve the same purpose:


$ sudo ifconfig | Grep-c Inet6


7. Search for a matching line number in a file by a given string


The-n option of the grep command is a useful feature when you need to debug when compiling an error. It can tell you what line of content you are searching for in the file:


$ sudo grep-n "main" setup.py


8. Recursive search 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 for recursive search: $ sudo grep-r "function" *


9. Perform an exact match search


The-w option is passed to the grep command to perform an exact match search in the string: Contains the word to search for, not a wildcard. For example, enter as follows:


$ sudo ifconfig | Grep-w "RUNNING"


The line that contains the match within the quotation marks is printed out. Alternatively, you can try this:


$ sudo ifconfig | Grep-w "RUN"


When searching for this match, if there is no such a single word in the search, nothing will return.


10. Search in gzip compressed files


We also want to look at grep's derivative applications. The first one is Zgrep, which is similar to zcat and can be used for gzip-compressed files. It has command options similar to grep and is used in the same way:


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


11. Matching regular Expressions in files


Egrep is another derivative application that represents "extending the global regular expression". It can recognize more regular expression metacharacters, such as at +? | and (). Egrep is a very useful tool when searching for source code files, and there are other pieces of fragmented code files that need to be searched, making such search capabilities necessary. You can enable it by using option-E in the grep command.


$ sudo grep-e


12. Search for a fixed match string


Fgrep is used to search for a fixed-style string in a file or file list. function with Grep-f. A common usage of fgrep is to pass a file containing a style to it:


$ sudo fgrep-f file_full_of_patterns.txt file_to_search.txt


This is just the beginning of the grep command, and you may have noticed that it is simply too useful to implement a variety of requirements. In addition to this one-line command that we run, grep can also be written as a cron task or an automated shell script to execute. Stay curious, experiment with the options on the man page and write some grep expressions for your purposes.

In order to facilitate the exchange of learning, we specially set up the Marco Linux operation and maintenance of the Exchange group, to facilitate the exchange of learning, there are currently 300 small partners in the study, QQ group number: 576095618

This article is from the "Marco Linux Training" blog, so be sure to keep this source http://mageedu.blog.51cto.com/4265610/1972825

You're not using regular? See 12 examples of GREP commands in Linux

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.