JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Linux extraction command grep (Part 3)

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

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

Option 3

-i is case insensitive to the search string

$ grep Mail /etc/passwd
# 大写的M检索的结果为空,因为默认是区分大小写的
$ grep Mail –i /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
# -i 使其不区分大小写

-w lowercase w. Forces matching of whole words

$ grep mail –w /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
# 这时只有一行数据

-x lowercase x. Forces the entire line to match

$ grep mail –x /etc/passwd
# 结果为空

-f specifies the file where the search string is located, and reads the content of each line of the file as the search string.

document/reg.txt

mail
nobody

Use this file as the one specified by -f

$ grep –f /reg.txt /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

Of course, the content in the file can also be a regular expression

For example, change the content nobody to ^nobody (starting with nobody)

$ grep –f /reg.txt /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

We see that there is one missing piece of data in the result.

-E specifies the search string as a regular expression pattern

$ grep mail /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
$ grep ‘(mail)’ /etc/passwd
# 结果为空 这里grep将’(mail)’作为一个字符串来进行检索,检索到的结果为空
$ grep –E ‘(mail)’ /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
# 使用-E 指定’(mail)’为正则表达式,所以检索出来两条数据

-e specifies a string as the search condition for the search content. The difference is that you can specify multiple strings.

$ grep mail nobody /etc/passwd   # 我们的原意是想在passwd中检索mail和nobody
grep: nobody: No such file or directory
/etc/passwd:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
/etc/passwd:mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
# 但是我们看报错了,这时我们可以用-e来指定
$ grep –e mail –e nobody /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

Let's see the results are out. Isn't it very useful?

-G specifies that the search condition is a basic regular expression

-P specifies that the search condition is a Perl regular expression

Well, that's all about grep. If there is anything missing, please leave a message to supplement it. I hope this article is helpful to you.

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