linux中Grep常用的15个例子,Linux中Grep惯用的15个例子

Linux中Grep常用的15个例子

Grep命令主要用于从文件中查找指定的字符串。

首先建一个demo_file:

$ cat demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.

And this is the last line.

例01:从单个文件中查找指定的字符串

$ grep "this" demo_file

this line is the 1st lower case line in this file.

Two lines above this line is empty.

例02:从多个文件中查找指定的字符串

$ cp demo_file demo_file1

$ grep "this" demo_*

demo_file:this line is the 1st lower case line in this file.

demo_file:Two lines above this line is empty.

demo_file:And this is the last line.

demo_file1:this line is the 1st lower case line in this file.

demo_file1:Two lines above this line is empty.

demo_file1:And this is the last line.

例03:忽略大小写使用 grep -i

$ grep -i "the" demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

This Line Has All Its First Character Of The Word With Upper Case.

And this is the last line.

例04:在文件中匹配正则表达式

如果你能在实际使用正则表达式,能使效率大大提高。在下面的例子中,匹配了所有以lines开头,以empty结尾的行。

$ grep "lines.*empty" demo_file

Two lines above this line is empty.从Grep文档的来看,一个正则表达式必须遵循下面的匹配操作。

?         The preceding item is optional and matched at most once.

*          The preceding item will be matched zero or more times.

+         The preceding item will be matched one or more times.

{n}      The preceding item is matched exactly n times.

{n,}     The preceding item is matched n or more times.

{,m}    The preceding item is matched at most m times.

{n,m}  The preceding item is matched at least n times, but not more than m times.

例05:用grep -w来查找全匹配,不包括子字符串

比如说:用下面的例子搜索出来的例子包括"is","his"

$ grep -i "is" demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.

And this is the last line.

而用grep -iw搜索出来的结果如下: 注意,忽略大小。"IS","is"

$ grep -iw "is" demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

Two lines above this line is empty.

And this is the last line.例06:

例07:

例08:

例09:

例10:

例11:

例12:

例13:

例14:

例15:

原文链接: http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值