Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the grep command in Linux

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about the use of the grep command in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Grep is a powerful file pattern search tool pre-installed with every Linux distribution. Whatever the reason, if your system doesn't have it preinstalled, 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 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 are 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 which version of Python your Ubuntu installer has installed on your system, or which modules it installs. 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 to the command grep-I python, which can be simply interpreted as transferring the result to grep, filtering out all items that contain 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.

two。 Search and filter files

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

There is something wrong with your Apache web server and you have to find a post from many professional websites to ask. The person who replied to you kindly asked you to paste up the contents of your / etc/apache2/sites-available/default-ssl file. If you can remove all the comment lines, isn't it easier to find the problem for you, for the people who help you, and for all who read the file? 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 not to print out matching items, but to do the opposite and print out all mismatched items. In this example, there are # comment lines. In fact, this command is not accurate, and the lines containing "#" are not all comment lines. You can learn more about regular expressions about how to match comment lines exactly. ).

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 various formats. You need to find all the mp3 music files of the artist jayZ, and don't have any mixed tracks in it. You can do this magic by using the find command and using grep in conjunction with pipes:

$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, then pipe them to grep-I to filter and print out the file named "JayZ", and then pipe it to grep-vi to filter out items containing "remix".

35 practical examples of find commands in Linux

4. Display the line number before or after the search string

The other two options are the switch between-An and-B, which is used to display matching lines and line numbers, which control the number of lines displayed before or after the string, respectively. The Man page gives a more detailed explanation, and I found a memory trick:-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 similar to that in example 4, but instead of printing lines before or after the matching string, it prints lines that match in both directions.

$sudo ifconfig | grep-C 2 lo

6. Calculate the number of matches

This function is similar to piping the result of grep output to the counter (wc program), and the built-in option of grep 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 very useful feature when you need to debug when you make a compilation error. It tells you which line of the file you are searching for:

$sudo grep-n "main" setup.py

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

9. Do an exact match search.

Pass the-w option to the grep command to perform an exact match search in a string. For example, enter something like this:

$sudo ifconfig | grep-w "RUNNING"

Lines containing matches within quotation marks are printed. In addition, 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 be returned.

10. Search in Gzip compressed files

Let's also take a look at the derivative application of grep. * one is zgrep, which is very 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. Match regular expressions in a file

Egrep is another derivative application that represents "extended global regular expressions". It can recognize more regular expression metacharacters, such as at +? | and (). Egrep is a very useful tool when searching source code files, and there are other search requirements for fragmentary code files that make such a search capability necessary. You can enable it with the option-E in the grep command.

$sudo grep-E

twelve。 Search for a fixed matching string

Fgrep is used to search for fixed-style strings in a file or list of files. The function is the same as that of 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, which you may have noticed is simply too useful for implementing a variety of requirements. In addition to the 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 options on the man page, and write some grep expressions for your purposes.

Thank you for reading! This is the end of this article on "what is the use of grep commands in Linux?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report