Home  >  Article  >  php教程  >  Linux basic command text filtering grep

Linux basic command text filtering grep

高洛峰
高洛峰Original
2016-11-08 14:49:381202browse

It is often necessary to filter text or output content in Linux. The most commonly used filtering command is grep

grep [OPTIONS] PATTERN [FILE...]

grep retrieves each input line line by line. If the input line contains the pattern PATTERN, this line is output. The PATTERN here is a regular expression (refer to the previous article, this article will use grep as an example).

Output the line containing root in the file /etc/passwd:

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

Or get it from the 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

What needs to be noted is that when grep’s input comes from both a file and standard input, grep will ignore the standard input The content will not be processed unless the symbol - is used to represent the 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

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

Output the lines starting with root in the file /etc/passwd and the file /etc/group:

[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
(标准输入):root:x:0:0:root:/root:/bin/bash
(标准输入):operator:x:11:0:operator:/root:/sbin/nologin

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

Output the lines starting with root in the file /etc/passwd and the file /etc/group:

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

Output the lines ending with /bin/bash in the file /etc/passwd:

[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 the PATTERN in the above two examples Quoted in double quotes to prevent parsing by the shell.

Output the lines in the file /etc/passwd that do not start with any of the letters in a-s:

[root@centos7 temp]# grep "^[^a-s]" /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

You need to understand the different meanings between the two ^ here. The first ^ represents the beginning of the line, and the second one is inside [] The first character ^ means negation.

Output the lines in the file /etc/passwd where the character 0 appears three times or more in a row (note the escape character ''):

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

For example, output the lines starting with the character r or l in the file /etc/passwd:

[root@centos7 temp]# grep "^[r,l]" /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]#

Option -o means to output only matching characters, not the entire line:

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

Option -c counts the number of matching lines:

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

Option -v Indicates inverse matching, such as outputting lines in /etc/passwd that do 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

Option -f FILE means using each line in the file FILE as a pattern match:

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

Option -x means the entire line Match:

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

Option-w means to match the entire word:

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

Option-h means not to output the file name 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 means to display line numbers:

[root@centos7 temp]# grep -n "^[r,l]" /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

Option-A N, -B N , -C N means output matching lines and their 'surrounding lines'

-A N 表示输出匹配行和其之后(after)的N行
-B N 表示输出匹配行和其之前(before)的N行
-C N 表示输出匹配行和其之前之后各N行
[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 the characters), which is equivalent to executing the command fgrep:

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

The 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 special meanings of characters can be expressed without escaping, including ?, +, {, |, (and).

The option -P means to use perl's regular expression for matching
For example:

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

"d" in perl regular expression means a number, and + means matching one to multiple times (same as vim).

Option -a treats binary files as text files:

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

Options --exclude=GLOB and --include=GLOB mean to exclude and include files matching GLOB respectively, GLOB means wildcard (see the basic command introduction for find and xargs usage) 3):

[root@centos7 temp]# find . -type f | xargs grep --exclude=*.txt --include=test* bash
./test.sh:#!/bin/bash
[root@centos7 temp]#

grep’s powerful filtering capabilities come from the combination of various options and regular expressions

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