linux正则表达式

. 一个字符
… 两个字符
.* 至少一个
^ 开头
$ 结束
[abc] 包含a或b或c的都匹配 ,匹配单个字符
[^abc] 只要出现了abc这三个字母以外的字符就都匹配,排除
1 以a开头或以b开头或以c开头
[abc] 不以a开头或不以b开头或不以c开头
a+ 匹配至少一个或多个a
a* 匹配0或多个a

大写 [[:upper:]]  [A-Z]
小写 [[:lower:]]  [a-z]
字母 [[:alpha:]] [a-Z]   
字母数字 [[:alnum:]]
空格或者制表符 [[:blank:]]
纯数字 [[:digit:]] [0-9]
标点符号 [[:punct:]]

正则表达式,又称正规表示法、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。
在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。

许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。
正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。

grep 在文件里查找某个关键字

显示查出的行在原文件的行号加n参数 grep -n root /etc/passwd
反向查找加v参数 grep -v bin /etc/passwd
ps -ef | grep sshd | grep -v grep
大小写不敏感加i参数 grep -ni root grep.txt

扩展grep

egrep “root|ftp|adm” /etc/passwd --同时查找多个关键字

grep -E “root|ftp|adm” /etc/passwd

统计root在/etc/passwd里出现了几次
grep -o root /etc/passwd |wc -l

cat 1.txt

111
222
333
444
555
666

cat 2.txt

aaa
bbb
333
ccc
ddd
555

grep -f 1.txt 2.txt --找出两个文件里重复的行

333
555

diff 1.txt 2.txt --查找不同

ifconfig |grep -A 1 vmnet

vmnet1 Link encap:Ethernet HWaddr 00:50:56:C0:00:01
inet addr:1.1.1.1 Bcast:1.1.1.255 Mask:255.255.255.0

vmnet8 Link encap:Ethernet HWaddr 00:50:56:C0:00:08
inet addr:192.168.56.1 Bcast:192.168.56.255 Mask:255.255.255.0

查找出有rot或者是rat的行 grep -n r[oa]t grep.txt --注意的是,[]括号内不论几个字符,都是选一个

查找一个非r字符加oot连在一起的行 grep ‘[^r]oot’ grep.txt

查找非小写字母连着一个oo的行 grep ‘[^a-z]oo’ grep.txt

–记住: [] 里的^为取反 ,外面的为以它开头

grep 2[oa]t grep.txt

grep [^a-z] grep.txt
查找非全部为小写字母的字符串行
查找至少包含一个非小写字母的字符行

grep ‘3oot’ grep.txt

root
boot

grep ‘[^a-z]oot’ grep.txt

Root
Boot

cat test

root
Root
rot
boot
Rot
111
222
,123
.456
haha
hello
A1234
A123
A12
A1

grep ‘[^A-Z]o’ test --查找二个小写字符一起的字符串,第一个是任意字符,第二个是o

root
Root
rot
boot

grep ‘[^A-Z]oo’ test

root
boot

grep ‘[A-Z]’ test --不以大写开头的行

root
rot
boot

grep ‘[a-z]’ test --不以小写开头的行

查找不以大写字母开头 grep ‘[[:upper:]]’ grep.txt
grep ‘[A-Z]’ grep.txt
grep -v ‘4’ grep.txt

查找有数字的行 grep ‘[0-9]’ grep.txt
或者 grep [[:digit:]] grep.txt

查找一个数字和一个字母连起来的行

        grep -E '[0-9][a-Z]|[a-Z][0-9]' grep.txt

查找不以r开头的 grep -v ^r grep.txt
grep [r] grep.txt

查找以数字开头的 grep 5 grep.txt
grep 6 grep.txt

查找以大写字母开头的 grep 7 grep.txt 或者 grep 8 grep.txt
查找以小写字母开头的 grep 9 grep.txt 或者 grep 10 grep.txt

查找以点结束的 grep “.”$ grep.txt --注意要引起来,而且要转义
grep “*”$ grep.txt

去掉空格行 cat grep.txt |grep -v ^$ --以^$表示空格行

查找完全匹配abc的行 grep ^abc$ grep.txt

查找到A后有三个数字的行 egrep A[0-9]{3}

cat test

root
Root
rot
boot
Rot
111
222
,123
.456
haha
hello
A1234
A123
A12
A1
A

egrep A[0-9]* test --查找A后有0到多个数字 *表示前一个字符有0个或多个

A1234
A123
A12
A1
A

egrep “A[0-9][0-9]*” test --查找A后有1到多个数字,所以相对于上面的显示,最后一行没有了(等同于egrep -n “A[0-9]+” grep.txt)

A1234
A123
A12
A1

egrep A[0-9]+ test – +号意思是前一个字符有1个或多个

A1234
A123
A12
A1

-----------------------------

. 点号代表一个任意字符

  • 代表零个或者多个前字符
  • 代表一个或者多个前字符
    .* 代表0个或多个任意字符
    …* 代表非空的任意字符

cat grep.txt

ggle
gogle
google
gooogle
gagle
gaagle
gaaagle
abcgef
abcdef
goagle
aagoog
wrqsg

grep g. grep.txt --g后边有一个任意字符
grep g* grep.txt --结果比较怪
grep “g*” grep.txt --结果比较怪
grep g.g grep.txt --g和g之间有一个任意字符
grep gg grep.txt --包含1个或多个g的行
grep go.g grep.txt
grep go.g grep.txt
grep go
g grep.txt
grep go…g grep.txt
grep go.
.g grep.txt
grep g
.*g grep.txt


  1. abc ↩︎

  2. br ↩︎

  3. a-z ↩︎

  4. A-Z ↩︎

  5. 0-9 ↩︎

  6. [:digit:] ↩︎

  7. A-Z ↩︎

  8. [:upper:] ↩︎

  9. a-z ↩︎

  10. [:lower:] ↩︎


版权声明:本文为liutao261311原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。