grep用法

grep是一个常用的命令。
以前只知道^$.+?*这些用法。以及-E的正则表达式。

今天偶然发现还有一些更高级的匹配模式。具体如下:

[:alnum:]    字母与数字字符
[:alpha:]    字母
[:ascii:]    ASCII字符
[:blank:]    空格或制表符
[:cntrl:]    ASCII控制字符
[:digit:]    数字
[:graph:]    非控制,非空格字符
[:lower:]    小写字母
[:print:]    可打印字符
[:punct:]    标点符号字符
[:space:]    空白字符,包括垂直制表符
[:upper:]    大写字母
[:xdigit:]    十六进制数字

还有一些常用的就是
{n}            必须匹配n次
{n,}        必须匹配n次或n次以上
{n,m}        必须匹配n到m次,包含n和m

下面我们进行简单的测试下就知道了
文本内容如下test.txt

A novel is a book of long narrative in literary prose

The genre has historical roots both in the fields of the medieval and early modern romance and in the tradition of the novella

The latter supplied the present generic term in the late 18th century.

Further definition of the genre is historically difficult.

The construction of the narrative, the plot, the way reality is created in the works of fiction.

Most of these requirements were introduced in the 16th and 17th  centuries in order to give fiction a justification outside the field of  factual history.

The individualism of the presentation makes the  personal memoir and the autobiography the two closest relatives among  the genres of modern histories.
grep a[[:blank:]] test.txt

5561066808_a504984507_z.jpg

grep -E [a-z]\{10\} test.txt

5561066804_ae4c0ebe60_z.jpg

grep Th.[[:space:]] test.txt

5561066798_fb5625ab54_z.jpg

###########################################

Best regards
Timo Seven
blog:http://www.timoseven.com
twitter: http://twitter.com/zauc
Linux System Admin  & MySQL DBA

关于 Timo
XNIX SA & MYSQL DBA

4 Responses to grep用法

  1. zjx说道:

    全部不能
    zsh: no matches found: a[[:blank:]]
    zsh: no matches found: [a-z]{10}
    zsh: no matches found: Th.[[:space:]]
    换成bash也没有输出

    • timo说道:

      我在bash下完全可以,你的不会是bash版本过老吧。上面几个都测试过
      bash –version
      GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: