grep

1.正则表达式php


1.1 基本正则表达式(RE)git

 

匹配 释义
元字符 ^
行首锚定
$
行尾锚定
.
任意单个字符
*
不表明字符(属于限定符),匹配其前导字符的任意次
[ ]
字符集匹配
[^ ]
不匹配其中列出的任意字符
\(\)
后续表达式经过“转义序列”(\1)来引用
x\{m,n\}
匹配字符x出现的次数区间;表示最少时须要逗号
\<
词首锚定
\>
词尾锚定
组合
.*
这个组合表示任意字符的任意长度
e*
表示空,所有匹配(由于星号表示任意数量,也包括零)
\?
匹配其前字符 1 次或 0 次
b\{1,1\}
匹配字母“b” 1 次
b\{2,3\} 匹配字母“b” 最少 2 次,至多 3 次
b\{3\} 匹配字母“b”3次
b\{,3\} 错误
b\{3,\} 匹配字母“b” 最少 3 次
b\{0,3\}a
i_f02.gif

 

匹配行首开始的年份“1983-02”、“1983-02-06_17:33:26”:web

$ vlinkstatus -a | grep "^[[:digit:]]\{4\}-\([[:digit:]]\{2\}\)"
$ vlinkstatus -a | grep "^[[:digit:]]\{4\}-[[:digit:]]\{2\}-[[:digit:]]\{2\}_[[:digit:]]\{2\}\(:\)[[:digit:]]\{2\}\1[[:digit:]]\{2\}"

“.”表明一个字符,不能省略。没法匹配下面的“file”。
正则表达式

[web@h p]$ cat re.txt
file
file1
file22
file3 rc
[web@h p]$ grep "file.$" re.txt
file1
[web@h p]$ grep "file." re.txt
file1
file22
file3 rc
[web@h p]$

 

在文件“re.txt”追加一行“file4 ”,末尾是个空格。bash

[web@h p]$ echo "file4 " >> re.txt
[web@h p]$ grep "file.$" re.txt
file1
[web@h p]$ grep "file. $" re.txt
file4 
[web@h p]$ grep "file..$" re.txt
file22
file4

 

1.2 扩展正则表达式(ERE)ide

元字符 释义
① - 与基本正则表达式意义、用法彻底相同
+
匹配其前导字符最少一次(属于限定符)
?
前导字符最多出现一次(属于限定符);零次或者一次
|
表示多个表达式之间或的关系
( )
表示一组可选值的集合;一般与竖线一块儿使用、不只于此

 

[web@h p]$ egrep "file[[:digit:]]+" re.txt
file1
file22
file3 rc
file4 
[web@h p]$ egrep "file[[:digit:]]?" re.txt
file
file1
file22
file3 rc
file4 
[web@h p]$ egrep "e(1|3)" re.txt
file1
file3 rc
[web@h p]$

 

1.3 Perl正则表达式spa

 

元字符 释义
\d

匹配从0到9中的任意一个数字字符;“[0-9]”命令行

\D

匹配一个非数字字符;“[^0-9]”翻译

\s 匹配任意空白字符 ;空格、制表……,“[\f\n\r\t\v]”
\S 匹配任意非空白字符

 

[web@h p]$ grep -P "e\d" re.txt
file1
file22
file3 rc
file4 
[web@h p]$ grep -P "e\d\s" re.txt
file3 rc
file4

 

1.4 空白字符code

任意空白字符 ”,命令行体验一下;包括“\f \n \r \t \v”。

 

表达式 意义 翻译
\f form feed 换页
\n new line 换行

\r

carriage return 回车
\t horizontal tab 水平制表
\v vertical tab 垂直制表

 

[web@h p]$ echo -e "hello\fworld"
world             # 这个命令执行后,就清屏了,只剩下个“world”了。
[web@h p]$ echo -e "hello\nworld"
hello
world
[web@h p]$ echo -e "hello\rworld"
world
[web@h p]$ echo -e "hello\tworld"
hello    world
[web@h p]$ echo -e "hello\vworld"
hello
     world
[web@h p]$ echo -e "hello\vvworld"
hello

     world

 

1.5 字符集

字符类 释义
[:alpha:] 匹配一个字母;“[a-zA-Z]”
[:digit:] 匹配一个数字;“[0-9]”
[:alnum:] 匹配一个字母或数字;“[0-9a-zA-Z]”
[:lower:] 小写字母
[:upper:] 大写字母
[:space:] 一个空白字符;空格、制表符、换行……
[:blank:] 空格、制表符
[:graph:] 一个看得见的可打印字符;不包括空白字符
[:print:] 大于上边,包括空白字符;不包括控制字符、字符串结束符“\0”、EOF文件结束符(-1)
[:cntrl:] 匹配一个控制字符(ASCII字符集中的前32个字符)
[:punct:] 匹配一个标点符号
[:xdigit:] 匹配十六进制数字

 

2.grep


 

基本语法
grep [option] pattern [file]...

 

选项 说明
输出控制

-c

只打印匹配的行数
--color 高亮显示匹配到的内容
-o 只显示被匹配到的字符串
-h 处理多个文件时,不显示文件名
-l 只显示匹配到的文件名称(跟上边相反)
-n

显示匹配到的行,并显示行号

-q 不输出匹配结果;而依据程序退出状态判断匹配与否
匹配控制
-i 忽略大小写匹配
-v 显示不匹配的文件行
-w
匹配整个单词
-x
匹配整个文本行
-F 不支持正则表达式
-E
支持扩展正则表达式
-P
支持perl正则表达式
上下行控制

-A "n"

匹配行往下显示n行
-B "n"
匹配行往上显示n行
-C "n"
匹配行往上、下显示n行
文件和目录选择
-r 递归搜索全部的目录下的文件

 

直接打印出符合条件的行数,而不用另外调用“wc -l”:

$ grep docBase server.xml -c
1

 


 

  1. eg.1.全文搜索:
    #!/bin/bash
    
    #tree -if $1 | grep ".php$"
    echo -e '     path:\t' $1
    echo -e '   string:\t' $2
    echo -e 'extension:\t' $3
    echo ""
    
    for i in $(tree -if $1 | grep ".${3}$")
    do
        if [ -f $i ]
        then
            #echo "hello " $i
            #grep --color -l $2 $i && echo "filename: " $i
            grep --color -l $2 $i
            grep --color $2 $i
            #echo grep -q $2 $i && echo -n "match: "$(grep -c $2 $i) && echo -e "\tfilename: " $i
        fi
    done
    View Code

     

 

grep.

相关文章
相关标签/搜索