Have you ever encountered a need to look up a specific string or style in a file, but don't know where to start? Then, please grep to help you.
Grep is a powerful file-mode search tool pre-installed for every Linux distribution. 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
I've found that using real-world examples in the real world to get you involved is the easiest way to get in touch with grep commands.
1. Search and find files
Suppose 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.4Interactive high-level object-oriented language (version2.7)
Ii python2.7-minimal2.7.3-0ubuntu3.4Minimal subset of the Python language(version2.7)
Ii python-openssl 0.12-1ubuntu2.1 Python wrapper around the OpenSSL library
Ii python-pam 0.4.2-12.2ubuntu4APython interfacetothe 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 us look at one such scenario:
There is a problem with your Apache web server, 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:
$ sudo grep -v "#" /etc/apache2/sites-available/default-ssl
The option -v tells the grep command to reverse its output, meaning that no matching items are output, and the opposite is done, printing out all the unmatched items. In this example, there is a comment line with # (note: this command is not accurate, the line containing "#" is not a comment line. For more information on how to match the comment line accurately, you can learn more about regular expressions. ).
3. Find out all the mp3 files
The grep command is very useful for filtering results from standard output. For example, suppose you have a folder of music files in various formats. You need to find all the music files in the mp3 format of the artist jayZ, and don't have any mixed audio tracks inside. Use the find command and then use the grep with the pipeline to complete 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 .mp3 suffix, then pipe it to grep -i to filter and print the file named "JayZ", and then pipe it to grep - Vi to filter out items that contain "remix".
4. Display the line number before or after the search string
The other two options are the switch between -A and -B, which is used to display the matching line and line number, respectively controlling the number of lines displayed before or after the string. The Man page gives a more detailed explanation. I found a little trick of memory: -A=after, -B=before.
$ sudo ifconfig | grep -A 4 etho $ sudo ifconfig | grep -B 2 UP
5. Print the line number around the matching string
The -C option of the grep command is very similar to the one in Example 4, but it does not print the line before or after the matching string, but prints the lines that match both directions (Annotation: same as the memory above) :-C=center, centered on this: $ sudo ifconfig | grep -C 2 lo
6. Calculate the number of matches
This function is similar to pipelining the output of grep to a counter (wc program). The built-in option of grep can achieve the same purpose:
$ sudo ifconfig | grep -c inet6
7. Search for the matching line number in the file by the given string
The -n option of the grep command is a very useful feature when you need to debug when compiling errors. It tells you which line of the file the search is in:
$ 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 to facilitate recursive search: $ sudo grep -r "function" *
9. Perform an exact match search
Passing the -w option to the grep command allows an exact match search in the string (translation: contains the word to search, not the wildcard). For example, type something like this:
$ sudo ifconfig | grep -w “RUNNINGâ€
A line containing the match within the quotes will be printed. In addition, you can also try this:
$ sudo ifconfig | grep -w "RUN"
When searching for this match, if there is no such single word in the search, nothing will be returned.
10. Search in Gzip archives
We also need to pay attention to the derivative application of grep. The first one is zgrep, which is very similar to zcat and can be used for gzipped 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. Match regular expressions in the file
Egrep is another derivative application that stands for "Extended Global Regular Expressions". It recognizes more regular expression metacharacters, such as at + ? | and (). When searching for source code files, egrep is a very useful tool, and there are other search requirements for fragmented code files, making such search capabilities necessary. You can enable it with the 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. The function is the same as grep-F. A common use 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 for implementing a wide variety of needs. In addition to this one-line command we run, grep can also be written as a cron task or an automatic shell script to execute. Be curious, experiment with the various options on the man page, and write some grep expressions for your purposes.
Shenzhen Innovative Cloud Computer Co., Ltd. , https://www.xcypc.com