grep

 

 

linux grep命令

1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

2.格式 grep [OPTIONS] PATTERN [FILE...]

PATTERN:格式

-i :不区分大 小写(只适用于单字符)。

grep -i 'root' /etc/passwd

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

--color:显示颜色

grep --color 'root' /etc/passwd

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

-v:显示没有匹配到的行

grep -v 'root' /etc/passwd

bin:x:1:1:bin:/bin:/sbin/nologin

........

-o:只显示搜寻的字符串

grep -o 'root' /etc/passwd

root
root
root
root

grep 'r..t' /etc/passwd

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

operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

grep 'a.*b' 1.txt

*: 匹配其前面的字符任意次

ab
aab
acb
adb
amnb
amnbmnbmnb

grep 'a\?b' 1.txt

\?: 匹配其前面的字符1次或0次

b
ab
aab
acb
adb
amnb
grep 'a\{1,3\}b' 1.txt

\{m,n\}:匹配其前面的字符至少m次,至多n次

ab

aab

位置锚定:

^:锚定行首,此字符后面的任意内容必须出现在行首

$:锚定行尾,此字符前面的任意字符必须出现在行尾

^$:空白行

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2014-12-07 19:32  tianx  阅读(185)  评论(0编辑  收藏  举报