$ grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
$ grep --color 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
grep -i #不区分大小写
grep -v #没有匹配的显示
grep -o #只显示被匹配到的字符串本身
grep正则表达式
元字符:
. :匹配任意单个字符
$ grep --color 'r..t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
* 匹配其前面的字符任意次(包括0次)
a*b a出现任意次,其后跟的b
.* :任意长度任意字符
$ grep 'a.*b' grep.txt
ab
aab
acb
adb
amnb
abmmvnbmvnb
$ grep '.*b' grep.txt
b
ab
aab
acb
adb
amnb
abmmvnbmvnb
$ grep '.*b' grep.txt | wc -l
7
$ grep 'a.*b' grep.txt | wc -l
6
$ grep 'a.*b' grep.txt
ab
aab
acb
adb
amnb
abmmvnbmvnb
$ grep 'a*b' grep.txt
b
ab
aab
acb
adb
amnb
abmmvnbmvnb
?: 匹配其前面字符1次或0次
$ grep --color 'a\?b' grep.txt #有一部分符合就可以
b
ab
aab
acb
adb
amnb
abmmvnbmvnb
{m,n}: 匹配其前面的字符至少m次,至多n次
$ grep --color 'a\{1,1\}b' grep.txt
ab
aab
abmmvnbmvnb
$ grep --color 'ab' grep.txt
ab
aab
abmmvnbmvnb
$ grep --color 'a\{1,2\}b' grep.txt
ab
aab
abmmvnbmvnb
$ grep --color 'a.\{1,2\}' grep.txt
ab
aab
acb
adb
amnb
abmmvnbmvnb
网友评论