Home  >  Article  >  Operation and Maintenance  >  Comprehensive explanation of the usage of grep command under Linux

Comprehensive explanation of the usage of grep command under Linux

齐天大圣
齐天大圣Original
2020-09-16 16:12:303425browse

To learn the Linux system, you must master the grep command. The grep command is used to find matching lines in files or standard output. Its power is that it supports regular expressions. In daily work, grep is definitely one of the most used commands.

Note: This article only introduces the usage of grep, and does not introduce regular expressions.

Let’s take a look at some of its common options and their meanings:

Options

  • ##-i Ignore case when searching

  • -n List all matching lines and display the line number

  • -v Reverse selection

  • -c Only output the number of matching lines

  • -l Only list the file names that match, do not list specific matching lines

  • -h Does not display file names when querying multiple files

  • -r Recursive search

  • -q Disables output of any results, exited status indicates search Is it successful?

  • --color=auto Display the found keywords with color

  • -E, --extended-regexp PATTERN Yes An extensible regular expression (abbreviated as ERE)

  • -B, --before-context=NUM Print NUM lines starting with text

  • -A, --after-context=NUM Print NUM lines ending with text

Here are some uses of grep:

We know php .ini has a time zone configuration. The details are not clear. I only know timezone. Now if we want to find the configuration information, we can use grep to find it

# 加上-i选项,忽略大小写
# grep -i timezone /usr/local/php/etc/php.ini 
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = PRC

grep supports searching in multiple files

# 这里我们加上-n选项,输出文件的行号
# grep -in stdio itoa.c quicksort.c 
itoa.c:2:#include <stdio.h>
quicksort.c:1:#include <stdio.h>

The reverse selection -v is used. When I run a program and want to see if the program still exists in the process, I can use the following method

# ps aux | grep curl.php 
root     14374 98.3  1.2 277844 12396 pts/0    R+   07:54   1:07 php curl.php
root     14404  0.0  0.0 112664   984 pts/2    R+   07:55   0:00 grep --color=auto curl.php

There is a problem here, we You need to use -v to filter out the process of grep itself

# ps aux | grep curl.php | grep -v grep 
root     14374 98.5  1.2 277844 12396 pts/0    R+   07:54   2:36 php curl.php

We want to count the number of lines containing root in the last command, so we need to use the -c option here.

# last | grep -c root
353

There is often a need to replace certain text in files in batches, so how do you know which files these texts are in. grep can do it, with the -l option

# find . -type f -exec grep -l define {} \;
./find.c
./itoa2.c
./wc.c
./test.c
./wordcnt.c
./longestline.c
./cal.c
./sortline2.c
./sortline.c
./found.c
./atof.c

Let’s look at another example. This example uses the -E extended regular rule. We want to filter out the comment lines and blank lines in the php.ini file

grep -vE &#39;^;|^$&#39; /usr/local/php/etc/php.ini

There is also a commonly used option -r, which can be used to recursively search all files in the current directory and subdirectory files. Next, we want to check what footer files are in the website directory. We can use the following command to find

grep -rn footer ./

The above is the detailed content of Comprehensive explanation of the usage of grep command under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn