文章目录

  • 前言
  • 正则表达式
  • grep命令用法
  • grep命令实例
  • 用法一:查找root字符串的行数以及内容
  • 用法二:查找"core id"字符串以及前后2行内容
  • 用法三:匹配包含"sh"字符串的内容
  • 用法四:匹配以"sh"结尾前面有0到2个字符的字符串
  • 用法五:匹配以"sh"结尾前面有0到2个字符的英文单词
  • 用法六:查询以"h"结尾的字符串
  • 用法七:查询当前目录下的所有文件中包含关键”hello2“ 的文件,以及行数


前言

Linux的“三剑客”指的是:grepsedawk
之所以被称为三剑客是通过上述工具可以更好的处理linux的查询结果。

正则表达式

上述命令之所以被称为三剑客是因为他们能很好的结合正则表达式来处理查询内容,并且只有上述“三剑客”能结合正则表达式使用。下面介绍以下正则表达式组成字符的含义:

元字符

功能

说明

^

匹配首行

表示以某个字符开头

$

匹配行尾

表示以某个字符结尾

^$

空行的意思

表示空行的意思

.

匹配任意单个字符

表示匹配任意单个字符

*

匹配0个或多个此字符

表示重复的任意多个字符

\

转义字符

表示转义字符

[]

匹配中括号内的字符

表示过滤括号内的字符

.*

代表任意多个字符

表示匹配任意多个字符

特殊的几个如下:

正则表达式

解释

.

任意一个字符。

[abc]

表示匹配一个字符,这个字符必须是abc中的一个。

[a-zA-Z]

表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个。

[^123]

匹配一个字符,这个字符是除了1、2、3以外的所有字符。

对于一些常用的字符集,系统做了定义:

字符集

等价系统定义

[A-Za-z]

等价于 [[:alpha:]]

[0-9]

等价于 [[:digit:]]

[A-Za-z0-9]

等价于 [[:alnum:]

tab,space

等空白字符 [[:space:]]

[A-Z]

等价于 [[:upper:]]

[a-z]

等价于 [[:lower:]]

标点符号

[[:punct:]]

匹配次数:

\{m\}

匹配其前面出现的字符m次

\{m,\}

匹配其前面出现的字符至少m次

\{m,n\}

匹配其前面出现的字符至少m次,至多n次

\?

匹配其前面出现的内容0次或1次,等价于{0,1}

*

匹配其前面出现的内容任意次,等价于{0,},所以 “.*” 表述任意字符任意次,即无论什么内容全部匹配。

三剑客的功能非常强大,但我们只需要掌握他们分别擅长的领域即可:grep擅长查找功能sed擅长取行和替换awk擅长取列

grep命令用法

用法:

grep [选项]... PATTERN [FILE]...

列出一些常见的选项命令

选选项

解释

–color

对匹配到的文本着色显示

-v

反过来(invert),显示不配patern匹配到的行

-i

忽略大小写(ignore case)

-n

显示匹配的行号

-c

统计匹配的行数

-o

只显示被模式匹配到的字符串

-q

静默模式,不输出任何信息

-A

–after-context=NUM 打印以文本结尾的NUM 行

-B

–before-context=NUM 打印以文本起始的NUM 行

-C

–context=NUM 打印输出文本NUM 行

-e

实现多个选项之间的逻辑or关系,例如:grep -e ‘cat’ -e ‘dog’ file

-w

被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker

-E

开启扩展(Extend)的正则表达式,使用正则相当于egrep

-F

不支持正则,相当与fgrep

grep命令实例

用法一:查找root字符串的行数以及内容

[root@hadoop-master test-grep]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@hadoop-master test-grep]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@hadoop-master test-grep]# grep -vc "root" /etc/passwd
43
[root@hadoop-master test-grep]# grep -o "root" /etc/passwd
root
root
root
root

用法二:查找"core id"字符串以及前后2行内容

[root@hadoop-master test-grep]# grep -A 2 "core id" /proc/cpuinfo
core id		: 0
cpu cores	: 1
apicid		: 0
[root@hadoop-master test-grep]# grep -B 2 "core id" /proc/cpuinfo
physical id	: 0
siblings	: 1
core id		: 0
[root@hadoop-master test-grep]# grep -C 2 "core id" /proc/cpuinfo
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0

用法三:匹配包含"sh"字符串的内容

[root@hadoop-master test-grep]# grep "/.*sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
setroubleshoot:x:990:986::/var/lib/setroubleshoot:/sbin/nologin
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

用法四:匹配以"sh"结尾前面有0到2个字符的字符串

[root@hadoop-master test-grep]# grep "/.\{0,2\}sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

用法五:匹配以"sh"结尾前面有0到2个字符的英文单词

[root@hadoop-master test-grep]# grep -w ".\{0,2\}sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

用法六:查询以"h"结尾的字符串

[root@hadoop-master test-grep]# grep "h$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

用法七:查询当前目录下的所有文件中包含关键”hello2“ 的文件,以及行数

[root@hadoop-master shell-test]# grep -nr "hello2"  ./
./test-grep/file.txt:leo2 hello2
./test-grep/file2.text:leo2 hello2 txt2