1) 搜索特定字符串
含有the并且输出他的行号
[softlib@localhost biaoShiYan]$ grep -n 'the' filename/ 8:I can't finish the test.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
[softlib@localhost biaoShiYan]$ grep -in 'the' filename /寻找the,不区分大小写
8:I can't finish the test.
9:Oh! The soup taste good.
12:the symbol '*' is represented as start.
14:The gd software is a library for drafting programs.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
[]的使用
[softlib@localhost biaoShiYan]$ grep -n 't[ae]st' filename
8:I can't finish the test.
9:Oh! The soup taste good.
[softlib@localhost biaoShiYan]$ grep -n '[^g]oo' filename
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
2) ^和$
[softlib@localhost biaoShiYan]$ grep -n '^the' filename
12:the symbol '*' is represented as start.
[softlib@localhost biaoShiYan]$ grep -n '^[^a-zA-Z]' filename
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird
[softlib@localhost biaoShiYan]$ grep -n '.$' filename
$ grep –n ‘^$’ express
$ grep –v ‘^$’ express | grep –v ‘^#’ //反向输出到 | 管道 再反向输出没有#开头的。
[softlib@localhost biaoShiYan]$ grep -v '^$' filename |grep -v '^#'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
[softlib@localhost biaoShiYan]$
3) . 和* 点:是任意字符的意思。 *是任意前面一样的字符 0个也行。
[softlib@localhost biaoShiYan]$ grep -n 'g..d' filename
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".
[softlib@localhost biaoShiYan]$ grep -n 'ooo*' filename
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!
[softlib@localhost biaoShiYan]$ grep -n '[0-9][0-9]*' filename
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
4) {}
[softlib@localhost biaoShiYan]$ grep -n 'o/{2/}' filename
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!
[softlib@localhost biaoShiYan]$ grep -n 'go/{2,5/}g' filename
18:google is the best tools for search keyword.
g后两或两个以上的o
[softlib@localhost biaoShiYan]$ grep -n 'go/{2,/}g' filename
18:google is the best tools for search keyword.
19:goooooogle yes!
5) 保存匹配的字符串:/(…/)
将捕获的正则表达式匹配的字符串存储在编号为1到9的“寄存器”中。
[softlib@localhost biaoShiYan]$ grep -n 'o/{2/}' filename
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!
$ grep –n ‘^/(./)’ express 匹配行首的第一个字符
$ grep –n ‘^/(./)/1’ express 如果一行的头两个字符相同,就匹配他们
$ grep –n ‘^/(./).*/1$’ express 匹配一行中头一个字符跟最后一个字符相同的行。
6) 正则表达式与命令行特殊符号的区别
$ ls –l * ls | grep –n ‘.*’ zhuren
[softlib@localhost biaoShiYan]$ ls -l filename | grep -n '.*'
1:-rw-rw-r--. 1 softlib softlib 646 3月 11 15:43 filename
[softlib@localhost biaoShiYan]$ ls -l filename
-rw-rw-r--. 1 softlib softlib 646 3月 11 15:43 filename
$ ls – a* ls | grep –n ‘^a.*’
$ ls – [!a]*
7) Sed命令
$ echo The tiger cubs will meet on Tuesday after school | sed 's/tiger/wolf/' The wolf cubs will meet on Tuesday after school