Linux system grep command operation example summary

  

Is still a headache for not knowing how to use grep command, if you pass the instance, you can better grasp the usage of grep command, the following small series will introduce you to Linux using grep command For example, I hope to help you.

grep is a powerful Linux distribution preinstalled each file pattern search tool. For whatever reason, if your system does not pre-install it, you can easily install it through the system's 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

Instance:

1. Search and find files assuming you You have installed a brand new Ubuntu on your computer, and then you plan 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 which version of Python your Ubuntu installer has installed on your system, or which modules it has installed. To solve this trouble, 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 the pipeline to transfer the output to the command grep -i python. This step can be simply interpreted as passing the result to grep and filtering out all the items containing python and returning the result. The –i option is used to ignore case because grep is case sensitive. Using the option -i is a good habit unless you plan 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 find a post from many professional websites. The person who kindly responds to you asks you to paste the contents of your /etc/apache2/sites-available/default-ssl file. If you can remove all the comment lines, isn't it easier for you, the person who helped you, and all the people reading the file to find the problem? Of course you can do it easily! Just do this:
Previous12Next page Total 2 pages

Copyright © Windows knowledge All Rights Reserved