grep 参数使用和实例

来源:互联网 发布:数据库物理模型主键 编辑:程序博客网 时间:2024/04/30 23:39
转自cublog:http://blog.chinaunix.net/uid-26738765-id-3130001.html
一、grep 参数使用
Gun grep   选项
-b   在搜索到的行的前面打印该行所在的块号码。
-c   只显示有多少行匹配 ,而不具体显示匹配的行
-h   不显示文件名
-i    在字符串比较的时候忽略大小写
-l    只显示包含匹配模板的行的文件名清单,不同项目之间用换行符分隔
-L   打印不匹配模板的文件名清单
-n   在每一行前面打印该行在文件中的行数
-s   静默工作,除非出现错误信息否则不打印任何信息,这个功能在检测退出状态的时候有用
-v   反检索,只显示不匹配的行
-w 
-Ax  在匹配指定行打印完毕后,再打印x行(向原文件匹配行下x行)
-By  在匹配指定行前面打印y行(在原文件匹配行上面打印y行)
-Cz  在匹配行前后打印z行  (在原文件匹配行上下打印z行)
-b    在每一行前面打印字符偏移量
-f  file   从文件file中提取模板。空文件中包含0个模板
-q     取消标准输出,跟-n功能是一样的
-s     不显示关于不存在或者无法读文件的错误信息
-w   只打印以单词形式匹配模板的行,模板可以是包含数字、字符和下划线的字符串
-x    只打印整行匹配的行
-y   用法同-i
-U  把文件作为二进制文件,这个选项只在MS-DOS和MS-Windows中被支持(这个参数没有明白,请过路高人指点,非常感谢)
-u 按照unix风格报告字符偏移量。只在-b选项同时被使用的时候才有效。这个选项只在MS-DOS和MS-Windows中被支持
 
grep "$name" file  把变量$name 的值作为模板,在文件中寻找匹配模板的行。注意,必须使用双引号
 
重复作用的元字符, \{\}; 用来做标签的元字符,\(\); 用来锚定单词的元字符\<\>
 
二、实例
数据文件
[root@future tmp]# cat newbo
1  aa
2  AA
3  Aa
4
5
[root@future tmp]# cat bo 
1
2
sss   bo-
3
4
5.8 shell  5      5
6
hello grep 5
 
[root@future root]# grep -n "" bo
1:1
2:2
3:sss   bo-
4:3
5:4
6:5.8 shell  5      5
7:6
8:hello grep 5
 
[root@future tmp]# grep -i "a" newbo       -i关闭大小写敏感
1  aa
2  AA
3  Aa
 
[root@future tmp]# grep -l '2' *                -l 打印匹配的文件名,而不打印匹配的行
bo
newbo
 
[root@future tmp]# grep -c 'a' newbo     打印有多少匹配行
2
[root@future tmp]# grep -c -i 'a' newbo
3
 
[root@future tmp]# grep -w 'shell' bo   -w打印按照单词方式匹配模板的行,而不是作为单词的一部分匹配模板的行
5.8 shell  5      5
[root@future tmp]# grep -w 'hell' bo
[root@future tmp]#
 
[root@future tmp]# grep -2 '3' newbo      上下各两行
1  aa
2  AA
3  Aa
4
5
[root@future tmp]# grep -A2 '3' newbo  下两行
3  Aa
4
5
[root@future tmp]# grep -B2 '3' newbo   上两行
1  aa
2  AA
3  Aa
[root@future tmp]# grep -C2 '3' newbo    上下各两行
1  aa
2  AA
3  Aa
4
5
[root@future tmp]#
 
[root@future tmp]# grep -b '' newbo   打印每一行前打印字符偏移量(不明白具体指的是什么感觉比较抽象,在次请高人指教)
0:1  aa
6:2  AA
12:3  Aa
18:4
20:5
 
[root@future tmp]# cat regular 
1
2
[root@future tmp]# grep -f regular newbo   -f 是指从文件中读取模板 
1  aa
2  AA
[root@future tmp]#
 
[root@future tmp]# grep '' *
bo:1
bo:2
bo:sss   bo-
bo:3
bo:4
bo:5.8 shell  5      5
bo:6
bo:hello grep 5
newbo:1  aa
newbo:2  AA
newbo:3  Aa
newbo:4
newbo:5
regular:1
regular:2
[root@future tmp]# grep -h '' *    -h 使得grep不打印头信息,这个例子中是不打印文件名
1
2
sss   bo-
3
4
5.8 shell  5      5
6
hello grep 5
1  aa
2  AA
3  Aa
4
5
1
2
 
[root@future tmp]# grep -q '' " newbo   -q取消grep的所有输出,在只需要退出状态值的场合这个选项就显得非常有用
[root@future tmp]# echo $?
0
[root@future tmp]#
 
 
 
 
 
 
数据文件
[root@future root]# cat bo
1
2
sss   bo-
3
4
5.8 shell  5      5
6
hello grep 5
[root@future root]# cat nu
1
ss   -bo
2
xx    bo-
2222222222
3
4
 
[root@future root]# grep 2 * 在所有的文件中搜索“2”
bo:2
nu:2
nu:2222222222
 
[root@future root]# grep '^5' *        ^锚定行的开始
bo:5.8 shell  5      5
 
[root@future root]# grep '5$' bo      $锚定行的结尾
5.8 shell  5      5
hello grep 5
 
[root@future root]# grep '5\..' bo     第一个字符是5,然后紧跟着一个. 尔后是一个任意字符
5.8 shell  5      5
如果点前面有一个反斜杠,则点就不再特殊,而是仅仅表示一个点。
 
[root@future root]# grep "^[23]" bo  表示开头是以2或3开头的行
2
3
 
[root@future root]# grep [^0-9] bo       打印所有包含非数字字符的行
sss   bo-
5.8 shell  5      5
hello grep 5
[root@future root]# grep '[^0-9]' bo
sss   bo-
5.8 shell  5      5
hello grep 5
[root@future root]# grep "[^0-9]" bo
sss   bo-
5.8 shell  5      5
hello grep 5
 
[root@future root]# grep "^[^0-9]" bo    打印所有不是以数字开头的行
sss   bo-
hello grep 5
 
[root@future root]# grep '[a-z]\{5\}' bo   打印每个字符串至少有5个连续小写字母的字符串的行
5.8 shell  5      5
hello grep 5
[root@future root]# grep '^[a-z]\{5\}' bo打印开头字符串至少有5个连续小写字母的字符串的行
hello grep 5
 
[root@future root]# grep '\(5\)\.[0-9].*\1 *\1' bo  打印第一个字符是5,紧跟着一个句点,然后是任意一个数字,然后是任意字符,然后是一个5,然后是一个任意个制表符,然后又是一个5。
5.8 shell  5      5
 
[root@future root]# grep '\(5\)\.[0-9] \1 *\1' bo 之所以没有搜索到是因为没有“.*”任意字符
[root@future root]# grep '\(5\)\.[0-9] \1 .*\1' bo
[root@future root]# grep '\(5\)\.[0-9] .* \1 *\1' bo
5.8 shell  5      5
\1 是一个引用,含义是正则表达式中第一个被\(和\)括起来的部分
 
[root@future root]# grep '\<hel' bo       锚定单词的开头
hello grep 5
 
[root@future root]# grep '5\>' bo         锚定单词的结尾
5.8 shell  5      5
hello grep 5
 
[root@future root]# grep '\<hel\>' bo
[root@future root]# grep '\<hello\>' bo    打印所有包含单词hello的行
hello grep 5
 
[root@future root]# grep 'hello' bo 
hello grep 5
[root@future root]# grep '\hello\b' bo   \b 是单词分界符  没有理解这个参数请路过的高人注解
hello grep 5
 
[root@future root]# grep '^h\w*\w' bo   也没有理解\w参数,仍然请过路高人注解,万分感激!
hello grep 5
 
[root@future root]# grep '\<[a-z].*o\>' bo              .* 代表任意字符
sss   bo-
hello grep 5
 
[root@future root]# grep '\<[a-z]\{4\}o\>' bo        重复4次,加上前面一次,共5次
hello grep 5

[root@future root]# grep '\<[a-z]\{5\}o\>' bo
[root@future root]# grep '\<[a-z]\{3,20\}o\>' bo
hello grep 5
[root@future root]# grep '\<[a-z]\{6,20\}o\>' bo
[root@future root]# grep '\<[a-z]\{5,20\}o\>' bo
[root@future root]# 
[root@future root]# grep '\<[a-z]\{4,20\}o\>' bo  重复4次
hello grep 5
 
[root@future root]# grep '\<[a-z]\{3,5\}o\>' bo   
hello grep 5
[root@future root]# grep '\<[a-z]\{5,6\}o\>' bo
[root@future root]#
 
注:a\{x,y\}\  意义是重复a+(x和y之间含x和y)
 
 
[root@future root]# grep '2|3' bo        grep不支持扩展的正则表达式,竖线是用于表示‘或’的扩展正则表达式元字符。所以没有输出。
[root@future root]# grep '2\|3' bo      加上反斜杠,这个字符就被翻译成扩展正则表达式。
2
3
[root@future root]# grep "\(2\|3\)" bo
2
3
 
 
[root@future root]# grep "\(5\)\.8.*\1" bo
5.8 shell  5      5
正则表达式5.8被匹配,模板5 就被存储到内存中的寄存器1内,这个正则表达式的含义是如果5.8被找到,标记并保存5,然后搜索任意个字符(.*),尔后是一个\1代表5
[root@future root]# grep "\(5\).*\1" bo
5.8 shell  5      5
 
 
[root@test-linux tmp]# cat te
adlkf adfkl aa        566.5
dfad 234.43 aaa
234           aaaa
adf adfa adf adf 45.556  aaaaa
[root@test-linux tmp]#
[root@test-linux tmp]#
[root@test-linux tmp]#
[root@test-linux tmp]# grep '\baaa\b' te      \b 单词分界符
dfad 234.43 aaa
[root@test-linux tmp]#
 
 
[root@test-linux tmp]# cat te
adlkf adfkl aa        566.5
dfad. 234.43 aaa
dfad . 234.43 aaa
234           aaaa
adf adfa adf adf 45.556  aaaaa
[root@test-linux tmp]#
[root@test-linux tmp]#
[root@test-linux tmp]#
[root@test-linux tmp]# grep '^d\w' te   \w 字母数字字符[a-zA-Z0-9]  \W 非字母数字字符
dfad. 234.43 aaa
dfad . 234.43 aaa
[root@test-linux tmp]#
原创粉丝点击