JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Linux extraction command grep (Part 2)

Author:JIYIK Last Updated:2025/04/08 Views:

This article follows up on Linux extraction command grep (Part 1) and continues to introduce the options and usage of the grep command.

Option 2

In this group, we mainly introduce options to control grep output information.

-m stops outputting when the number of lines to be displayed reaches the limit specified by this option. That is to say, if -m specifies that the maximum number of lines to be displayed is 3, and if the search result has 4 lines, only the first three lines will be displayed.

$ grep mail –m 1 /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin  # 只显示一行结果,通过前面的例子我们知道其实含有mail字符串的一共有两行数据。
# or
$ grep mail –max-count=1 /etc/passwd    # -m NUM  <=> --max-count=NUM
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin # 同样结果也是一行

-n displays the line number of each line of data before outputting the data of the retrieved string.

$ grep mail –n /etc/passwd
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
22:mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
# 这两行数据是其在/etc/passwd中所在的行号

-b matches the starting position of the lines of the string, which is calculated in bytes. This means that if there are 230 bytes of data before the line of data, the starting position of the line is 230.

$ grep mail –b /etc/passwd
293:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
877:mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

-o only display matching strings

$ grep mail –o /etc/passwd
mail
mail
mail
mail

-H adds the file name as a prefix to the displayed information. For searching a single file, the file name is not used as a prefix by default. This option adds the file name as a prefix.

$ grep mail –H /etc/passwd
/etc/passwd:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
/etc/passwd:mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

-h This option has the opposite function to -H, which is to extract the file name as a prefix. This option is used when searching multiple files, because single file search does not have a file name prefix by default.

$ grep mail /etc/passwd /etc/group
/etc/passwd:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
/etc/passwd:mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
/etc/group:mail:x:12:mail
/etc/group:mailnull:x:47:
# 多文件检索会在每一行前面加上改行所在的文件名作为前缀
$ grep mail –h /etc/passwd /etc/group
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
mail:x:12:mail
mailnull:x:47:
# 此时结果中就没有文件名的前缀了

-q does not display standard output information, even if the string is retrieved, it will not be displayed. This option is similar to -s. -s blocks standard error output, while this option blocks standard output information.

$ grep mail –q /etc/passwd
# 结果为空
$ grep mail –q /etc/passwd /etc/passwds
grep: /etc/passwds: No such file or directory
# 我们看,对于错误信息-q并不会屏蔽
$ grep mail –qs /etc/passwd /etc/passwds
# 什么也不显示,标准输出和标准错误输出都被屏蔽掉了

-c lowercase c, display the number of lines that match the specified string

$ grep mail –c /etc/passwd
2

-d ACTION If the input file is a directory, we need to use this option followed by ACTION to process it. The default value of ACTION is read, which means that the directory is read like a normal file; if ACTION is skip, the directory will be skipped; if ACTION is recurse, grep will read all the files in the directory as a data source (equivalent to grep's -r option). For recurse, let's take an example - search the contents of all files in the /etc directory and retrieve lines that start with mail

$ grep ^mail –d recurse /etc
/etc/passwd:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
/etc/passwd:mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
/etc/mail/helpfile:mail MAIL From:<sender> [ <parameters> ]
/etc/mail/helpfile:mail         Specifies the sender.  Parameters are ESMTP extensions.
/etc/mail/helpfile:mail         See "HELP DSN" for details.
……
# 等价于
$ grep ^mail –r /etc
# 结果相同。

We also introduced the use of the -r option. We also reviewed the use of the -h option. Since all files in the directory are searched, the file name will be prefixed to each line of results. You can use -h to remove it.

$ grep ^mail –hd recurse /etc  # 这里注意 h和d的顺序
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
mail    MAIL From:<sender> [ <parameters> ]
mail            Specifies the sender.  Parameters are ESMTP extensions.
mail            See "HELP DSN" for details.
mail.*                                                  -/var/log/maillog
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
mail:*:16619:0:99999:7:::
mailnull:!!:16619:0:99999:7:::
…..

-D ACTION This option is basically the same as -d, except that it is used when the input file is a device (FIFO or socket). Its ACTION is the same as -d, but without recurse. No more examples are given here.

-a retrieves data from binary files as text files

-I uppercase I ignore binary files

Search all files under pdo and retrieve the data containing main.

$ grep main –a –r /software/php-5.5.23 /ext/pdo 
# pdo下面的二进制文件会被当做普通文本文件来检索  相当于–binary-files=text。
$ grep main –binary-files=text –r /software/php-5.5.23 /ext/pdo
# 结果同-a相同

The binary files under pdo will be ignored

$ grep main –I –r /software/php-5.5.23 /ext/pdo
# 相当于 –binary-files=without-match
$ grep main –binary-files=without-match –r /software/php-5.5.23 /ext/pdo
# 结果同-I 相同

-L is the opposite of -l, it displays the file names that do not contain the search string. It is equivalent to --files-without-match

-l displays the file name of the file containing the search string data, which is equivalent to --files-with-matches

$ grep main -l -r /software/php-5.5.23/ext/ftp
/software/php-5.5.23/ext/ftp/package.xml
# 或者
$ grep main --files-with-matches -r /software/php-5.5.23/ext/ftp
# 结果同上
$ grep main –L –r /software/php-5.5.23/ext/ftp
/software/php-5.5.23/ext/ftp/php_ftp.h
/software/php-5.5.23/ext/ftp/tests/006.phpt
/software/php-5.5.23/ext/ftp/tests/ftp_exec_basic.phpt
/software/php-5.5.23/ext/ftp/tests/ftp_nb_get_large.phpt
/software/php-5.5.23/ext/ftp/tests/ftp_get_basic.phpt
….
# 部分结果,所有的显示结果中唯独没有/software/php-5.5.23/ext/ftp/package.xml这个文件
# 或者
$ grep main --files-without-match –r /software/php-5.5.23/ext/ftp

-A NUM adds NUM rows of data after the retrieved results. These rows of data are the NUM rows of data below the target row of data.

-B NUM is the opposite of -A, adding NUM rows of data to the front of the result

-C NUM Add NUM rows of data before and after the result

$ grep mail –B 1 /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
--
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
$ grep mail –A 1 /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
--
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
$ grep mail –C 1 /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
--
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin

This article will introduce these options first, and the rest of the options will be introduced in Linux extraction command grep (Part 3) .

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Restart PostgreSQL in Ubuntu 18.04

Publish Date:2025/04/09 Views:72 Category:PostgreSQL

This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n

Issues to note when installing Apache on Linux

Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM

As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial