#新建一个测试文件,下面会用到
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ cat testgrep wirelessqa is my blog wirelestqa is testfile wirelesmqa is testfile mmmqa is testfile mmqa mqa is testfile Wirelessqa is my blog my blog is wirelessqa my name is bixiaopeng bixiaopeng is my name bxp is my name my name is bxp my name is (bxp) b$##
#例:'^wirelessqa'匹配所有以wirelessqa开头的行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e ^wirelessqa testgrep wirelessqa is my blog #或 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e ^[wirelessqa] testgrep wirelessqa is my blog
#例:'wirelessqa$'匹配所有以wirelessqa结尾的行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e wirelessqa$ testgrep my blog is wirelessqa
#例:'wireles.qa'匹配wireles后接一个任意字符,然后是qa bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e wireles.qa testgrep wirelessqa is my blog wirelestqa is testfile wirelesmqa is testfile my blog is wirelessqa
#例1:' *qa'匹配所有一个或多个空格后紧跟qa的行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e m*qa testgrep wirelessqa is my blog wirelestqa is testfile wirelesmqa is testfile mmmqa is testfile mqa is testfile Wirelessqa is my blog my blog is wirelessqa #例2: .*一起用代表任意字符 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e .*qa testgrep wirelessqa is my blog wirelestqa is testfile wirelesmqa is testfile mmmqa is testfile mqa is testfile Wirelessqa is my blog my blog is wirelessqa
#例1: '[Ww]irelessqa'匹配Wirelessqa和wirelessqa bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e [Ww]irelessqa testgrep wirelessqa is my blog Wirelessqa is my blog my blog is wirelessqa #例子2:[m*qa]匹配以m开头的字符或以qa结尾包含一个或多个m的字符 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e [m*qa] testgrep wirelessqa is my blog wirelestqa is testfile wirelesmqa is testfile mmmqa is testfile mqa is testfile Wirelessqa is my blog my blog is wirelessqa my name is bixiaopeng bixiaopeng is my name bxp is my name my name is bxp
#例:'[^a-fh-m]qa'匹配不包含a-f和h-m的一个字母开头,紧跟qa的行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e [^a-fh-m]qa testgrep wirelessqa is my blog wirelestqa is testfile Wirelessqa is my blog my blog is wirelessqa
#例:匹配包含'\(bxp\)'的行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e \(bxp\) testgrep my name is (bxp)
例:'\<wire'匹配包含以wire开头的单词的行。 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e '\<wire' testgrep wirelessqa is my blog wirelestqa is testfile wirelesmqa is testfile my blog is wirelessqa
#例:'blog\>'匹配包含以blog结尾的单词的行。 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'blog\>' testgrep wirelessqa is my blog Wirelessqa is my blog my blog is wirelessqa
#例:'m\{3\}'匹配包含连续3个m的行。 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'm\{3\}' testgrep mmmqa is testfile
#例:'m\{2,\}'匹配至少连续有2个m的行。 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'm\{2,\}' testgrep mmmqa is testfile mmqa
#例:'m\{2,3\}'匹配连续2--3个m的行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'm\{2,3\}' testgrep mmmqa is testfile mmqa mmmmqa
#例:'b\w*p'匹配以b后跟零个或多个文字或数字字符,然后是p。 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'b\w*p' testgrep my name is bixiaopeng bixiaopeng is my name bxp is my name my name is bxp my name is (bxp)
#例: 匹配b后面为非[A-Za-z0-9]的行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'b\W' testgrep b$##
#例: '\bmqa\b'只匹配mqa,即只能是mqa这个单词,两边均为空格。 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e '\bmqa\b' testgrep mqa is testfile #单个\b也可以用,结果如下: bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e '\mqa\b' testgrep wirelesmqa is testfile mmmqa is testfile mmqa mqa is testfile
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e '^mmqa$' testgrep mmqa
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al | grep '^d..x..x..x' drwxr-xr-x+ 92 bixiaopeng staff 3128 Sep 21 15:33 . drwxr-xr-x 5 root admin 170 Jul 27 19:39 .. drwxr-xr-x 8 bixiaopeng staff 272 Jul 18 18:24 .Genymobile drwxr-xr-x 4 bixiaopeng staff 136 Dec 12 2012 .adobe …...
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep ^[^d] total 2856 -rw------- 1 bixiaopeng staff 5 Dec 8 2012 .CFUserTextEncoding -rw-r--r--@ 1 bixiaopeng staff 39940 Sep 21 10:22 .DS_Store -rw------- 1 bixiaopeng staff 147 Jun 18 09:20 .Xauthority -rw-r--r-- 1 bixiaopeng staff 12288 Jul 17 23:24 .bad.txt.swp