第二章Linux常用命令

 

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)   保存匹配的字符串:/(…/)
将捕获的正则表达式匹配的字符串存储在编号为19寄存器中。

[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

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值