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

How to use the linux basic command grep

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

Share

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

This article mainly explains how to use the linux basic command grep. 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 how to use the linux basic command grep.

In linux, you often need to filter text or output, and the most commonly used filtering command is grep

Grep [OPTIONS] PATTERN [FILE...]

Grep retrieves each line of input by line, and outputs this line if the input line contains the schema PATTERN. PATTERN here is a regular expression (refer to the previous article, this article will give an example in conjunction with grep).

The line in the output file / etc/passwd that contains root:

[root@centos7 temp] # grep root / etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

Or obtain from standard input:

[root@centos7 temp] # cat / etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

It is important to note that when the input of the grep comes from both the file and the standard input, grep ignores the standard input and does not process it unless the symbol-is used to represent the standard input:

[root@centos7 temp] # cat / etc/passwd | grep root / etc/passwd-/ etc/passwd:root:x:0:0:root:/root:/bin/bash / etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin (standard input): root:x:0:0:root:/root:/bin/bash (standard input): operator:x:11:0:operator:/root:/sbin/nologin

At this point, grep will indicate which results come from the file and which come from standard input.

Lines in the output file / etc/passwd and file / etc/group that begin with root:

[root@centos7 temp] # grep "^ root" / etc/passwd / etc/group / etc/passwd:root:x:0:0:root:/root:/bin/bash / etc/group:root:x:0:

The line in the output file / etc/passwd that ends with / bin/bash:

[root@centos7 temp] # grep "/ bin/bash$" / etc/passwd root:x:0:0:root:/root:/bin/bash learner:x:1000:1000::/home/learner:/bin/bash

Note that in the above two examples, PATTERN is quoted in double quotes to prevent it from being parsed by shell.

Lines in the output file / etc/passwd that do not begin with any of the letters in Amurs:

[root@centos7 temp] # grep "^ [^ amurs]" / etc/passwd tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin

Here you need to understand the different meanings between the two ^ s. * * ^ represents the beginning of the line, and the second * * character ^ inside [] indicates inversion.

Lines where the character 0 appears three or more times in the output file / etc/passwd (note the escape character'\'):

[root@centos7 temp] # grep "0\ {3,\}" / etc/passwd learner:x:1000:1000::/home/learner:/bin/bash

Such as lines in the output file / etc/passwd that begin with the characters r or l:

[root@centos7 temp] # grep "^ [rmeml]" / etc/passwd root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin learner:x:1000:1000::/home/learner:/bin/bash

Option-I causes grep to ignore case when matching patterns:

[root@centos7 temp] # grep-i abcd file ABCD function abcd () {[root@centos7 temp] #

The option-o means that only matching characters are output, not the entire line:

[root@centos7 temp] # grep-oi abcd file ABCD abcd [root@centos7 temp] #

Option-c counts the number of rows matched:

[root@centos7 temp] # grep-oic abcd file 2 [root@centos7 temp] #

The option-v indicates an inverse match, such as a line in the output / etc/passwd that does not end with / sbin/nologin:

[root@centos7 temp] # grep-v "/ sbin/nologin$" / etc/passwd root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt learner:x:1000:1000::/home/learner:/bin/bash

The option-f FILE means that every line in the file FILE is used as a pattern match:

[root@centos7 temp] # cat test abcd ABCD [root@centos7 temp] # grep-f test file ABCD function abcd () {[root@centos7 temp] #

The option-x indicates that the entire line matches:

[root@centos7 temp] # grep-xf test file ABCD [root@centos7 temp] #

The option-w matches the entire word:

[root@centos7 temp] # grep here file here there [root@centos7 temp] # grep-w here file here [root@centos7 temp] #

The option-h means that the file name is not output when there are multiple files:

[root@centos7 temp] # cat / etc/passwd | grep ^ root-/ etc/passwd-h root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash

Option-n indicates that the line number is displayed:

[root@centos7 temp] # grep-n "^ [rmenl]" / etc/passwd 1:root:x:0:0:root:/root:/bin/bash 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 24:learner:x:1000:1000::/home/learner:/bin/bash

The options-A N,-B N,-C N indicate the output matching line and its' surrounding line'

-A N indicates the output match line and the N line after (after)-B N indicates the output match line and the N line before (before)-C N indicates the output match line and the N lines before and after it [root@centos7 temp] # grep-A 2 ^ operator / etc/passwd operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14: 50:FTP User:/var/ftp:/sbin/nologin [root@centos7 temp] # grep-B2 ^ operator / etc/passwd halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin [root@centos7 temp] # grep-C1 ^ operator / etc/passwd mail:x:8:12:mail:/var/spool/mail:/sbin / nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin

Option-F treats PATTERN as its literal match (ignoring the special meaning of characters), which is equivalent to executing the command fgrep:

[root@centos7 temp] # grep-F ^ root / etc/passwd [root@centos7 temp] #

Command has no output

Option-E can use extended regular expressions, just like executing the egrep command:

[root@centos7 temp] # egrep "^ root | ^ learner" / etc/passwd root:x:0:0:root:/root:/bin/bash learner:x:1000:1000::/home/learner:/bin/bash

Using extended regular expressions means that there is no need to escape to express the special meaning of characters, including?, +, {, |, (and).

Option-P means to match using perl's regular expression

Such as:

[root@centos7 ~] # echo "helloworld123456" | grep-oP "\ d +" 123456 [root@centos7 ~] #

In the perl rule, "\ d" represents a number, and + indicates a match of one or more times (same as vim).

Option-a treats binaries as text files:

[root@centos7 ~] # grep-an online / usr/bin/ls% s online help: [root@centos7 ~] #

The options-- exclude=GLOB and-- include=GLOB indicate excluding and containing files that match GLOB, respectively, and GLOB represents wildcards (for the usage of find and xargs, see introduction to basic commands 3):

[root@centos7 temp] # find. -type f | xargs grep-- exclude=*.txt-- include=test* bash. / test.sh _ include=test* bash. [root@centos7 temp] # so far, I believe you have a better understanding of "how to use the basic linux command grep". 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