Linux命令之乐--grep
正则表达式基本组成部分
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-priority:59;
mso-style-unhide:no;
border:solid black 1.0pt;
mso-border-themecolor:text1;
mso-border-alt:solid black .5pt;
mso-border-themecolor:text1;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid black;
mso-border-insideh-themecolor:text1;
mso-border-insidev:.5pt solid black;
mso-border-insidev-themecolor:text1;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
|
正则表达式 |
描述 |
示例 |
|
^ |
行起始标记 |
^tux 匹配以tux起始的行 |
|
$ |
行尾标记 |
tux$ 匹配以tux结尾的行 |
|
. |
匹配任意一个字符 |
Hack. 可以匹配Hacki,但不可以匹配Hackia |
|
[] |
匹配[]中的任意一个字符 |
coo[kl] 可以匹配cook和cool |
|
[^] |
匹配除[^字符]任意一个字符 |
9[^01] 可以匹配92,93但不可以匹配90或91 |
|
[-] |
匹配指定范围的任意一个字符 |
[1-5] 匹配1~5中的任意一个数字 |
|
? |
匹配之前的项一次或零次 |
colou?r 匹配color或colour,不能匹配colouur |
|
+ |
匹配之前项1次或多次 |
no-9+ 匹配no-9,no-99,不能匹配no- |
|
* |
匹配之前的项0次或多次 |
co*l 匹配cl,col,cool等 |
|
() |
创建一个用于匹配的子串 |
ma(tri)?x 匹配max或matrix |
|
{n} |
匹配之前的项n次 |
[0-9]{3},匹配 |
|
{n,} |
之前的项至少匹配n次 |
[0-9]{2,} 匹配任意一个两位数或更多位的数 |
|
{n,m} |
匹配之前项目在n~m之间 |
[0-9]{2,5} 匹配任意的两位数或者五位数 |
|
| |
交替匹配两边任意一项 |
Oct (1st | 2nd) 匹配Oct 1st 或者Oct 2nd |
|
\ |
转义 |
a\.b 匹配a.b 不能匹配acb |
首先创建我们练习grep命令时需要用到的demo文件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.
1.从单个文件中搜索指定的字串
grep的基础用法是如下例的从指定的文件中搜索特定的字串。
语法:
grep "literal_string" filename
$ grep "this" demo_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.
2. 在多个文件中检索指定的字串
语法:
grep "string" FILE_PATTERN
先拷贝demo_file为demo_file1。grep的结果在符合条件的行前将包括文件名。当文件名包含元字符时,linux shell会将匹配的所有文件作为输入到grep中去。
$ 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.
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
如果你能有效地利用正则表达式,这是个很有用的特点。在下面的例子中,搜索全部以“lines”开始以“empty”结束的字串,如搜索“lines[之间任意字]empty” ,并且忽略大小写。
$ 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.
下例使用了-w选项,请注意结果中不包含 “This Line Has All Its First Character Of The Word With Upper Case”, 虽然 “This”中包含“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.
6. 使用grep -A, -B and -C显示之前、之后、前后的几行
当使用grep搜索大文件时,显示匹配行附近的多行数据是一个很有用的功能。
创建如下文件
$ cat demo_text
4. Vim Word Navigation 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
下例显示匹配行和之后的3行数据
$ 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行数据.
$ grep -C 2 "Example" demo_text
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
7.通过GREP_OPTIONS高亮显示搜索的字串
如果你希望搜索的字串高亮显示在结果中,可以试用以下的办法。
通过修改GREP_OPTIONS对搜索字串高亮显示。
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8' $ grep this demo_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.
8. 用grep -r递归搜索全部的文件
如果想查找当前目前以及其子目录的全部文件时,可以使用 -r 选项。如下例
$ grep -r "ramesh" *
9. 使用grep -v进行不匹配
可以使用-v选项显示不匹配搜索字串的行。下例显示demo_text文件中不包含“go”的行
$ grep -v "go" demo_text
4. Vim Word Navigation You may want to do several navigation in relation to the words, such as: 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.
10. 显示不匹配全部模式的行
语法:
grep -v -e "pattern" -e "pattern"
创建如下例子文件
$ cat test-file.txt
a
b
c
d $ grep -v -e "a" -e "b" -e "c" test-file.txt
d
11.用grep -c 统计匹配的行数
语法:
grep -c "pattern" filename
$ grep -c "go" demo_text
6
统计不匹配的行数
$ grep -v -c this demo_file
4
12. 用grep -l 只显示文件名
$ grep -l this demo_*
demo_file
demo_file1
13. 只显示匹配的字串
缺省显示匹配字串的所在行,可以使用-o选项只显示匹配的字串。这项功能当使用正则表达式时比较有用处。
$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
14. 显示匹配的位置
语法:
grep -o -b "pattern" file
$ cat temp-file.txt
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.
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-priority:59;
mso-style-unhide:no;
border:solid black 1.0pt;
mso-border-themecolor:text1;
mso-border-alt:solid black .5pt;
mso-border-themecolor:text1;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid black;
mso-border-insideh-themecolor:text1;
mso-border-insidev:.5pt solid black;
mso-border-insidev-themecolor:text1;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
Linux命令之乐--grep的更多相关文章
- 【linux命令】grep
1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局 ...
- linux命令之grep用法介绍
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- 每天一个linux命令(51)--grep命令
linux系统中grep 命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.grep 全称是 global regular expression print,表示全局正则 ...
- 【Linux命令】grep命令
1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...
- Linux命令——find/grep
这两个命令写起来会很多,这里只简单的写一些东西,加深自己的印象. 一.find find命令主要作用是沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作. 1)命令格式 find [参数] ...
- 每天学点Linux命令之grep 和 wc命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expr ession Print,表示全局正则表 ...
- 每日linux命令学习-grep模式检索
grep模式检索指令包括grep,egrep,和fgrep,.Linux系统使用正则表达式优化文本检索,所以在此,笔者首先学习了一下正则表达式. 1. 正则表达式 正则表达式使用被称为元字符(Meta ...
- linux命令系列 grep
grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE...] grep [ ...
- 每天学点Linux命令之grep 和 wc命令 ---(6/25)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
随机推荐
- C#处理文本文件TXT实例详解(转)
作者:安静平和 字体:[增加 减小] 类型:转载 时间:2015-02-02我要评论 这篇文章主要介绍了C#处理文本文件TXT的方法,以实例形式详细分析了txt文本文件的读取.修改及打印等功能的实现技 ...
- 三级级联查询省份名称和编码(保证名称不反复)的SQL语句
三级级联查询省份名称和编码(保证名称不反复)的SQL语句 1.省份.地市和县级数据库表 2.SQL语句 SELECT DISTINCT t.`province_name`,t.`province_co ...
- 梦想天空(关注前端开发技术 html5+css3)
http://www.cnblogs.com/lhb25/p/must-read-links-for-web-designers-and-developers-volume-32.html
- 【C#/WPF】键盘事件
需求:按下回车键,触发事件. 搜MSDN时,看到的键盘事件是System.Windows.Forms里的,在WPF中没法用: https://msdn.microsoft.com/zh-tw/libr ...
- [mmc]Linux下MMC/SD/SDIO的识别与操作
转自:http://blog.csdn.net/skdkjzz/article/details/38927943 我们知道host在扫描卡的过程中,其识别的顺序为SDIO SD MMC,并且从它的注 ...
- JVM 详谈
JVM 详谈 本来这次应该讲讲ORM 的几个框架,但是笔者还没有完全总结出来,所以这里先插入一次学习JVM的心得.作为一个Java程序员,如果不了解JVM的工作原理,就很难从底层去把 握Java语言和 ...
- 在传统以太网中,为什么要有最小帧长度(64 bytes)和最大帧长度(1500 bytes)的限制?
遇到的问题:以太网的数据帧封装如下图所示,包含在IP数据报中的数据部分最长应该是( )字节? A.1434 B.1460 C.1480 D.1500 答案:C 原因: 以太网(IEEE 802.3)帧 ...
- 深入理解MongoDB的复合索引
更新时间:2018年03月26日 10:17:37 作者:Fundebug 我要评论 对于MongoDB的多键查询,创建复合索引可以有效提高性能.这篇文章主要给大家介绍了关于MongoDB复 ...
- python + opencv: kalman 跟踪
之前博文中讲解过kalman滤波的原理和应用,这里用一个跟踪鼠标的例程来演示怎么在opencv里用自带的kalman函数进行目标跟踪,文章的内容对做图像跟踪有借鉴意义.文章主要是网络资源进行整理和简单 ...
- (转)SDL 1.2 to 2.0 Migration Guide--SDL1.2更新到SDL2.0指南
SDL 1.2 to 2.0 Migration Guide 目录 SDL 1.2 to 2.0 Migration Guide Translations Introduction Overview ...