Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19746996
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类:

2006-12-14 09:38:10

Reference:

1)        UNIX® Shells by Example, Third Edition

2)        Effective awk Programming, 3rd Edition

 

这部分需要在实际使用中琢磨添加, 其中参考资料2的还没有添加:

文档记录
1,    2006-12-14 9:38        结合实际和 书写草稿



实例:

匹配目录

[root@Meil89 bin]# ls -l |grep ^d

drwxr-xr-x    2 root     root         4096 Nov 20 15:05 meil_bak

 

只显示隐藏文件:

[root@Meil89 root]# ls -dl .*

drwxr-x---   12 root     root         4096 Nov 30 18:22 .

drwxr-xr-x   24 root     root         4096 Nov 23 19:15 ..

-rw-------    1 root     root        15648 Nov 30 09:42 .bash_history

-rw-r--r--    1 root     root           24 Jun 11  2000 .bash_logout

-rw-r--r--    1 root     root          317 Aug 26 10:55 .bash_profile

-rw-r--r--    1 root     root          267 Jun 13 10:47 .bash_profile~

-rw-r--r--    1 root     root          246 Apr  4  2006 .bash_profile.bak

-rw-r--r--    1 root     root          176 Aug 24  1995 .bashrc

-rw-r--r--    1 root     root          210 Jun 11  2000 .cshrc

drwx------    2 root     root         4096 Jun 26  2004 .gconfd

-rw-------    1 root     root        34152 Nov 22 14:36 .mysql_history

-rw-------    1 root     root         1024 Nov 20 17:54 .rnd

drwxr-xr-x    2 root     root         4096 Oct 26 11:11 .ssh

-rw-r--r--    1 root     root          196 Jul 11  2000 .tcshrc

-rw-------    1 root     root         8903 Nov 30 18:22 .viminfo

-rw-r--r--    1 root     root         2292 Jul  7 15:15 .vimrc

-rw-r--r--    1 root     root         1126 Aug 24  1995 .Xresources

疑问: ls –d参数总是觉得很难懂

 

[root@Meil89 root]# ls -dF .* --color=tty

./   .bash_history  .bash_profile   .bash_profile.bak  .cshrc    .mysql_history  .ssh/    .viminfo  .Xresources

../  .bash_logout   .bash_profile~  .bashrc            .gconfd/  .rnd            .tcshrc  .vimrc

这个命令的好处可以将颜色都显示出来.

[root@Meil89 root]# find ./ -type f -name "\.*"    

./.bash_logout

./.Xresources

./.viminfo

./.bash_profile

./.bashrc

./.cshrc

./.tcshrc

./.bash_history

./tcl/bin/.version_log.txt.swp

./tcl/cfg/.ip_2.2.2_new.cfg.swp

./mdr_agent_simu/.mdr_agent_simu.report.swp

./.bash_profile.bak

./.mysql_history

./.vimrc

./.rnd

./.bash_profile~

 

vi :

:1,$s/\<[Tt]om\>/David/g
从第1行到最后一行,查找所有Tomtom, 将其替换为David,其中s的意思为: substitute, gglobally
 
vi, ex, grep, egrep, sed, and awk中通用的正则表达式:

Table 2.1. Regular Expression Metacharacters

Metacharacter

Function

Example

What It Matches

^

Beginning-of-line anchor

/^love/

Matches all lines beginning with love.

$

End-of-line anchor

/love$/

Matches all lines ending with love.

.

Matches one character

/l..e/

Matches lines containing an l, followed by two characters, followed by an e.

*

Matches zero or more of the preceding characters

/ *love/

Match lines with zero or more spaces, followed by the pattern love.

[ ]

Matches one in the set

/[Ll]ove/

Matches lines containing love or Love.

[x–y]

Matches one character within a range in the set

/[A–Z]ove/

Matches letters from A through Z followed by ove.

[^ ]

Matches one character not in the set

/[^A–Z]/

Matches any character not in the range between A and Z.

\

Used to escape a metacharacter

/love\./

Matches lines containing love, followed by a literal period. Normally the period matches one of any character.

Additional metacharacters are supported by many UNIX programs that use RE metacharacters:

\<

Beginning-of-word anchor

/\

Matches lines containing a word that begins with love (supported by vi and grep).

\>

End-of-word anchor

/love\>/

Matches lines containing a word that ends with love (supported by vi and grep).

\(..\)

Tags match characters to be used later

/\(love\)able \1er/

May use up to nine tags, starting with the first tag at the left-most part of the pattern. For example, the pattern love is saved as tag 1, to be referenced later as \1; in this example, the search pattern consists of lovable followed by lover (supported by sed, vi, and grep).

x{m\}or x{m,\}or x{m,n\}

Repetition of character x, m times, at least m times, at least m and not more than n times[a]

o{5,10\}

Matches if line contains between 5 and 10 consecutive occurrences of the letter o (supported by vi and grep).

 
其他操作符:

+ This symbol is similar to *, except that the preceding expression must be matched at least once. This means that wh+y would match why and whhy, but not wy, wher eas wh*y would match all three of these strings. The following is

a simpler way of writing the last * example:

awk /\(c[ad]+r x\)/ { print } sample

 

以下把所有空格替换为 “,”

:1,$s/ \+/,/g
这在转换为*.csv文件中相当有用
 
 

具体实例见参考资料1.

 

:1,$s/\([0o]ccur\)ence/\1rence/

以上括号内容为第一部分,即查找Occur occur,其后为ence, 将其后面附加rence

 

1.   The editor searches for the entire string occurence or Occurrence (note: the words are misspelled), and if found, the pattern portion enclosed in parentheses is tagged (i.e., either occur or Occur is tagged). Since this is the first pattern tagged, it is called tag 1. The pattern is stored in a memory register called register 1. On the replacement side, the contents of the register are replaced for \1 and the rest of the word, rence, is appended to it. We started with occurence and ended up with occurrence.

以下例子:square and fair 改为 fair and square.

% vi textfile (Before Substitution)
    -------------------------------------------------------------
    Unusual occurrences happened at the fair.
    Patty won fourth place in the 50 yard dash square and fair.
    Occurrences like this are rare.
    The winning ticket is 55222.
    The ticket I got is 54333 and Dee got 55544.
    Guy fell down while running around the south bend in his last
    event.
    ~
    ~
    ~
1   :s/\(square\) and \(fair\)/\2 and \1/
    -------------------------------------------------------------
 
    % vi textfile (After Substitution)
    -------------------------------------------------------------
    Unusual occurrences happened at the fair.
--> Patty won fourth place in the 50 yard dash fair and square.
    Occurrences like this are rare.
    The winning ticket is 55222.
    The ticket I got is 54333 and Dee got 55544.
    Guy fell down while running around the south bend in his last
    event.

 

 

详细的参考:

shell-正则表达式语法 - -

                                      

 

正则表达式语法

一个正则表达式就是由普通字符(例如字符 a z)以及特殊字符(称为元字符)组成的文字模式。该模式描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。

 

\

将下一个字符标记为一个特殊字符、或一个原义字符、或一个 后向引用、或一个八进制转义符。例如,'n' 匹配字符 "n"'\n' 匹配一个换行符。序列 '\\' 匹配 "\" "\(" 则匹配 "("

 

^

匹配输入字符串的开始位置。

 

$

匹配输入字符串的结束位置。

 

*

匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo" * 等价于{0,}

 

+

匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"+ 等价于 {1,}

 

?

匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配 "do" "does" 中的"do" ? 等价于 {0,1}

 

{n}

n 是一个非负整数。匹配确定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o

 

{n,}

n 是一个非负整数。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o'o{1,}' 等价于 'o+''o{0,}' 则等价于 'o*'

 

{n,m}

m n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。 "o{1,3}" 将匹配 "fooooood" 中的前三个 o'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。

 

?

当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串 "oooo"'o+?' 将匹配单个 "o",而 'o+' 将匹配所有 'o'

 

.

匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。

 

(pattern)

匹配pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在Visual Basic Scripting Edition 中则使用 $0$9 属性。要匹配圆括号字符,请使用 '\(' '\)'

 

(?:pattern)

匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "" 字符 (|) 来组合一个模式的各个部分是很有用。例如, 'industr(?:y|ies) 就是一个比 'industry|industries' 更简略的表达式。

 

(?=pattern)

正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如, 'Windows (?=95|98|NT|2000)' 能匹配 "Windows 2000" 中的 "Windows" ,但不能匹配 "Windows 3.1" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。

 

(?!pattern)

负向预查,在任何不匹配Negative lookahead matches the search string at any point where a string not matching pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1" 中的 "Windows",但不能匹配 "Windows 2000" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始

 

x|y

匹配 x y。例如,'z|food' 能匹配 "z" "food"'(z|f)ood' 则匹配 "zood" "food"

 

[xyz]

字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'

 

[^xyz]

负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'

 

[a-z]

字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配 'a' 'z' 范围内的任意小写字母字符。

 

[^a-z]

负值字符范围。匹配任何不在指定范围内的任意字符。例如,'[^a-z]' 可以匹配任何不在 'a' 'z' 范围内的任意字符。

 

\b

匹配一个单词边界,也就是指单词和空格间的位置。例如, 'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'

 

\B

匹配非单词边界。'er\B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'

 

\cx

匹配由x指明的控制字符。例如, \cM 匹配一个 Control-M 或回车符。 x 的值必须为 A-Z a-z 之一。否则,将 c 视为一个原义的 'c' 字符。

 

\d

匹配一个数字字符。等价于 [0-9]

 

\D

匹配一个非数字字符。等价于 [^0-9]

 

\f

匹配一个换页符。等价于 \x0c \cL

 

\n

匹配一个换行符。等价于 \x0a \cJ

 

\r

匹配一个回车符。等价于 \x0d \cM

 

\s

匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]

 

\S

匹配任何非空白字符。等价于 [^ \f\n\r\t\v]

 

\t

匹配一个制表符。等价于 \x09 \cI

 

\v

匹配一个垂直制表符。等价于 \x0b \cK

 

\w

匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'

 

\W

匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'

 

\xn

匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如, '\x41' 匹配 "A"'\x041' 则等价于 '\x04' & "1"。正则表达式中可以使用 ASCII 编码。.

 

\num

匹配 num,其中 num 是一个正整数。对所获取的匹配的引用。例如,'(.)\1' 匹配两个连续的相同字符。

 

\n

标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进制数字 (0-7),则 n 为一个八进制转义值。

 

\nm

标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为后向引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足,若 n m 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm

 

\nml

如果 n 为八进制数字 (0-3),且 m l 均为八进制数字 (0-7),则匹配八进制转义值 nml

 

\un

匹配 n,其中 n 是一个用四个十六进制数字表示的 Unicode 字符。例如, \u00A9 匹配版权符号 (?)

阅读(1898) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~