grep的使用详情

grep使用方法:

grep -[acinv] '需要搜索的内容' filename

可选参数acinv说明:
-a 以文本文件方式搜索
-c 计算找到的符合行的次数
-i 忽略大小写
-n 顺便输出行号
-v 反向选择,即找非搜索内容的行

1.范例:

输出包含’the‘的行并输出行号

[xuemei.cao@gpu personal_test]$ grep -n 'the' test1
1:the world
3:Oh! the soup taste good!
5:google is the best tools for search keyword.
7:You are the best is menu you are the no.1.

输出不包含’the’的行并输出行号

[xuemei.cao@gpu personal_test]$ grep -nv 'the' test1
2:hello
4:apple is my favorite food.
6:However ,this dress is about $ 3183 dollars.
8:goooooogle yes!
9:
10:Five companies dominate 5G radio hardware and 5G systems for carriers
11:5G networks are digital cellular networks,
12:
2.利用[]搜索集合

1.[] 表示其中的某一个字符 ,例如[ade] 表示a或d或e

[xuemei.cao@gpu personal_test]$ grep -n 't[ae]st' test1
3:Oh! the soup taste good!
13:It's a test!

2.可以用^符号做[]内的前缀,表示除[]内的字符之外的字符。
比如搜索oo前没有f的字符串所在的行. 使用 ‘[^f]oo’ 作搜索内容

[xuemei.cao@gpu personal_test]$ grep -n '[^f]oo' test1 
3:Oh! the soup taste good!
5:google is the best tools for search keyword.
8:goooooogle yes!

3.[] 内可以用范围表示,比如[a-z] 表示小写字母,[0-9] 表示0~9的数字, [A-Z] 则是大写字母。[a-zA-Z0-9]表示所有数字与英文字符。当然也可以配合^来排除字符。

[xuemei.cao@gpu personal_test]$ grep -n '[0-9]' test1
6:However ,this dress is about $ 3183 dollars.
7:You are the best is menu you are the no.1.
10:Five companies dominate 5G radio hardware and 5G systems for carriers
11:5G networks are digital cellular networks,
3.行首与行尾字符 ^$

^ 表示行的开头,$表示行的结尾。那么‘^$’ 就表示空行
1.比如搜索the在开头的行

[xuemei.cao@gpu personal_test]$ grep -n '^the' test1
1:the world

2.搜索空行

[xuemei.cao@gpu personal_test]$ grep -n '^$' test1
9:
12:

3.搜索非小写字母开头的

[xuemei.cao@gpu personal_test]$ grep -n '^[^a-z]' test1
3:Oh! the soup taste good!
6:However ,this dress is about $ 3183 dollars.
7:You are the best is menu you are the no.1.
10:Five companies dominate 5G radio hardware and 5G systems for carriers
11:5G networks are digital cellular networks,
13:It's a test!

4.以s结尾的行

[xuemei.cao@gpu personal_test]$ grep -n 's$' test1
10:Five companies dominate 5G radio hardware and 5G systems for carriers
4. *通配符

在bash中*代表通配符,用来代表任意个字符,但是在正则表达式中,他含义不同,*表示有0个或多个某个字符。
例如 oo*, 表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.

[xuemei.cao@gpu personal_test]$ grep -n 'goo*' test1
3:Oh! the soup taste good!
5:google is the best tools for search keyword.
8:goooooogle yes!
[xuemei.cao@gpu personal_test]$  grep -n 'goooo*' test1
8:goooooogle yes!
5. 点. 代表一个任意字符,必须存在

g??d 可以用 ‘g…d’ 表示。good ,gxxd ,gabd …都符合

[xuemei.cao@gpu personal_test]$ grep -n 'g..d' test1
 3:Oh! the soup taste good!
6.限定连续重复字符的范围 { }

范围是数字用,隔开 {2,5} 表示2~5个,
{2}表示2个,{2,} 表示2到更多个
注意,由于{ }在shell中有特殊意义,因此作为正则表达式用的时候要用\转义。

搜索g后面跟2~5个o的行

[xuemei.cao@gpu personal_test]$ grep -n 'go\{2,5\}' test1  
3:Oh! the soup taste good!
5:google is the best tools for search keyword.
8:goooooogle yes!

参考博客链接:https://blog.csdn.net/tenfyguo/article/details/6387786

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值