# grep使用15招

608人阅读 评论(0)

### 1.从单个文件中搜索指定的字串

grep的基础用法是如下例的从指定的文件中搜索特定的字串。

语法：
grep "literal_string" filename

$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. ### 3. 用 grep -i 进行大小写无关的搜索 语法： grep -i "string" FILE 也是一个基本用法，对搜索的字串忽略大小写，因此下例中匹配“the”, “THE” and “The”。 $ 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.

### 4. 使用用正则表达式

语法：
grep "REGEX" filename

$grep -i "lines.*empty" demo_file Two lines above this line is empty. 正则表达式遵循的几个重复的操作 • ? 最多匹配一次 • * 匹配零次或者任意多次 • + 匹配一次以上 • {n} 匹配n次 • {n,} 最少匹配n次 • {,m} 最多匹配m次 • {n,m} 匹配n到m次 ### 5. 用grep -w搜索整个词，而不是词中的部分字串 使用-w选项搜索一个单词，并且避免搜索到词中的部分字串。 下例搜索"is"。如果不加-w选项，将显示“is”, “his”, “this” 等所有包含“is”的行。 $ 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" 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. ### 6. 使用grep -A, -B and -C显示之前、之后、前后的几行 当使用grep搜索大文件时，显示匹配行附近的多行数据是一个很有用的功能。 创建如下文件 $ cat demo_text

You may want to do several navigation in relation to the words, such as:

* e - go to the end of the current word.
* E - go to the end of the current WORD.
* b - go to the previous (before) word.
* B - go to the previous (before) WORD.
* w - go to the next word.
* W - go to the next WORD.

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

#### 6.1 显示匹配行之后的N行

-A

语法：
grep -A  "string" FILENAME

$grep -A 3 -i "example" demo_text Example to show the difference between WORD and word * 192.168.1.1 - single WORD * 192.168.1.1 - seven words. #### 6.2显示匹配行之前的N行 -B 语法： grep -B "string" FILENAME 下例显示匹配行和之前的2行数据 $ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

#### 6.3显示匹配行前后的N行

-C 显示之前的n行，之后的n行数据.

a
b
c
d

6

$grep -v -c this demo_file 4 ### 12. 用grep -l 只显示文件名 $ grep -l this demo_*
demo_file
demo_file1

12345
12345

$grep -o -b "3" temp-file.txt 0:3 6:3 注意: 以上输出显示的不是行内的位置，而是整个文件中的字节byte位置 ### 15. 用 grep -n 在输出时显示行号 行号从1开始 $ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：7126022次
• 积分：22054
• 等级：
• 排名：第323名
• 原创：268篇
• 转载：335篇
• 译文：0篇
• 评论：510条
博主写的书已经上市
个人资料
本人曾健生，家乡是佛山，现在广州工作，《App后台开发运维和架构实践》一书作者，曾经负责社交app "ekeo"和"米信"的后端开发，目前就职于云后端平台bmob从事云服务方面的开发工作（想了解bmob点击这里

qq：190678908
本人的微信公众号"app后端"
博客专栏
 app后端技术架构 文章：60篇 阅读：763251
最新评论