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

Summary of some grep commands commonly used in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains "the summary of some grep commands commonly used in Linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the summary of some grep commands commonly used in Linux.

Overview:

All linux-like systems provide a search tool called grep (global regular expression print, global regular expression output). The grep command is useful in the case of pattern-based searches for the contents of one or more files. The pattern can be a single character, multiple characters, a single word, or a sentence.

When the command matches the pattern specified when the command is executed, grep outputs a line containing the pattern, but does not modify the contents of the original file.

In this article, we will discuss 14 examples of grep commands.

Example 1 looks up the pattern (word) in the file

Look for the word "linuxtechi" in the / etc/passwd file

The code is as follows:

Root@Linux-world:~# grep linuxtechi / etc/passwd

Linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash

Root@Linux-world:~#

Example 2 looks for patterns in multiple files.

The code is as follows:

Root@Linux-world:~# grep linuxtechi / etc/passwd / etc/shadow / etc/gshadow

/ etc/passwd:linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash

/ etc/shadow:linuxtechi:$6$ DdgXjxlM$4flz4JRvefvKp0DG6re:16550:0:99999:7:::/etc/gshadow:adm:*::syslog,linuxtechi

/ etc/gshadow:cdrom:*::linuxtechi

/ etc/gshadow:sudo:*::linuxtechi

/ etc/gshadow:dip:*::linuxtechi

/ etc/gshadow:plugdev:*::linuxtechi

/ etc/gshadow:lpadmin:!::linuxtechi

/ etc/gshadow:linuxtechi:!::

/ etc/gshadow:sambashare:!::linuxtechi

Root@Linux-world:~#

Example 3 uses the-l parameter to list the file name of the file containing the specified pattern.

The code is as follows:

Root@Linux-world:~# grep-l linuxtechi / etc/passwd / etc/shadow / etc/fstab / etc/mtab

/ etc/passwd

/ etc/shadow

Root@Linux-world:~#

Example 4 uses the-n parameter to find the specified pattern in the file and display the line number of the matching line

The code is as follows:

Root@Linux-world:~# grep-n linuxtechi / etc/passwd

39:linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash

Root@Linux-world:~#

Root@Linux-world:~# grep-n root / etc/passwd / etc/shadow

Example 5 uses the-v parameter to output lines that do not contain the specified pattern

Output all lines in the / etc/passwd file without the word "linuxtechi"

The code is as follows:

Root@Linux-world:~# grep-v linuxtechi / etc/passwd

Example 6 uses the ^ symbol to output all lines that begin with a specified pattern

The Bash script treats the ^ symbol as a special character and is used to specify the beginning of a line or word. For example, all lines in the output / etc/passes file that start with "root"

The code is as follows:

Root@Linux-world:~# grep ^ root / etc/passwd

Root:x:0:0:root:/root:/bin/bash

Root@Linux-world:~#

Example 7 uses the $symbol to output all lines ending in the specified pattern.

Output all lines in the / etc/passwd file that end with "bash".

The code is as follows:

Root@Linux-world:~# grep bash$ / etc/passwd

Root:x:0:0:root:/root:/bin/bash

Linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash

Root@Linux-world:~#

The Bash script treats the dollar ($) symbol as a special character that specifies the end of a line or word.

Example 8 uses the-r parameter to recursively find a specific pattern

The code is as follows:

Root@Linux-world:~# grep-r linuxtechi / etc/

/ etc/subuid:linuxtechi:100000:65536

/ etc/group:adm:x:4:syslog,linuxtechi

/ etc/group:cdrom:x:24:linuxtechi

/ etc/group:sudo:x:27:linuxtechi

/ etc/group:dip:x:30:linuxtechi

/ etc/group:plugdev:x:46:linuxtechi

/ etc/group:lpadmin:x:115:linuxtechi

/ etc/group:linuxtechi:x:1000:

/ etc/group:sambashare:x:131:linuxtechi

/ etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash

/ etc/passwd:linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash

....

The above command will recursively look for the word "linuxtechi" in the / etc directory

Example 9 uses grep to find all blank lines in a file

The code is as follows:

Root@Linux-world:~# grep ^ $/ etc/shadow

Root@Linux-world:~#

Since there are no blank lines in the / etc/shadow file, there is no output

Example 10 uses the-I parameter lookup mode

The-I argument of the grep command ignores the case of characters when looking for them.

Let's look at an example and look for the word "LinuxTechi" in the paswd file.

[code] nextstep4it@localhost:~$ grep-I LinuxTechi / etc/passwd

Linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash

Nextstep4it@localhost:~$ [/ code]

Example 11 uses the-e parameter to find multiple patterns

For example, I want to look for the words' linuxtechi' and 'root' in a grep command, and with the-e argument, we can find multiple patterns.

The code is as follows:

Root@Linux-world:~# grep-e "linuxtechi"-e "root" / etc/passwd

Root:x:0:0:root:/root:/bin/bash

Linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash

Root@Linux-world:~#

Example 12 uses-f to specify the mode to be found with a file

First, create a search pattern file "grep_pattern" in the current directory, which I want to enter as follows.

The code is as follows:

Root@Linux-world:~# cat grep_pattern

^ linuxtechi

Root

False$

Root@Linux-world:~#

Now, try using the grep_pattern file to search

The code is as follows:

Root@Linux-world:~# grep-f grep_pattern / etc/passwd

Example 13 uses the-c parameter to calculate the number of pattern matches

Continuing with the example above, we use the-c command in the grep command to calculate the number of matches to the specified pattern

The code is as follows:

Root@Linux-world:~# grep-c-f grep_pattern / etc/passwd

twenty-two

Root@Linux-world:~#

Example 14 output matches the N lines before or after the specified pattern line

A) use the-B parameter to output the first four lines of the matching line

The code is as follows:

Root@Linux-world:~# grep-B 4 "games" / etc/passwd

B) use the-A parameter to output the last 4 lines of the matching line

The code is as follows:

Root@Linux-world:~# grep-A 4 "games" / etc/passwd

C) use the-C parameter to output 4 rows before and after matching lines

The code is as follows:

Root@Linux-world:~# grep-C 4 "games" / etc/passwd

At this point, I believe you have a deeper understanding of the "summary of some grep commands commonly used in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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