shell脚本之正则表达式2

上一篇 / 下一篇  2012-09-26 14:42:42

  grep精确匹配:在抽取字符串后加\>。
  grep  “48\>”  file
  15.grep消除大小写:加入-i选项
  grep  -I  “sept”  file
  16.特殊字符:$ . ‘ “ * [ ] ^ | \ + ?
  如果要查询这些字符,需要在前面加转义字符\。
  17.grep判断变量含有[HOST]字符串
  if [ "1" -eq "`echo "$VarName" | grep -c '\[HOST\]'`" ]; then
  18.grep判断变量含有[xxx]字符串
  if [ "1" -eq "`echo "$VarName" | grep -c '\[.*\]'`" ]; then
  19.grep匹配后缀为c,h,j,s,cpp,hpp的文件
  EXT_ALL='chjs'
  EXT_PP='ch'
  EXT_NO_PP='js'
  ls $1 | grep "\.[$EXT_ALL][p]\{0,2\}$" | grep -v  "\.[$ EXT_NO_PP][p]\{1,2\}$" | grep -v "\.[$ EXT_PP][p]\{1\}$"
  20.使用grep在文件中查找变量
  grep `echo $user` /etc/passwd | cut –f5 –d‘:’
  21.正则表达式语法
  来自msdn,仅供参考。
  Here are some examples of regular expressions:
 

   Expression

Matches

/^\s*$/

Match a blank line.

/\d{2}-\d{5}/

Validate an ID number consisting of 2 digits, a hyphen, and an additional 5 digits.

/<\s*(\S+)(\s[^>]*)?>[\s\S]*<\s*\/\1\s*>/

Match an HTML tag.

 
The following table contains the complete list of metacharacters and their behavior. in the context of regular expressions:
 

Character

Description

\

Marks the next character as a special character, a literal, a backreference, or an octal escape. For example, 'n' matches the character "n". '\n' matches a newline character. The sequence '\\' matches "\" and "\(" matches "(".

^

Matches the position at the beginning of the input string. If theRegExpobject'sMultilineproperty is set, ^ also matches the position following '\n' or '\r'.

$

Matches the position at the end of the input string. If theRegExpobject'sMultilineproperty is set, $ also matches the position preceding '\n' or '\r'.

*

Matches the preceding character or subexpression zero or more times. For example, zo* matches "z" and "zoo". * is equivalent to {0,}.

+

Matches the preceding character or subexpression one or more times. For example, 'zo+' matches "zo" and "zoo", but not "z". + is equivalent to {1,}.

?

Matches the preceding character or subexpression zero or one time. For example, "do(es)?" matches the "do" in "do" or "does". ? is equivalent to {0,1}

{n}

nis a nonnegative integer. Matches exactlyntimes. For example, 'o{2}' does not match the 'o' in "Bob," but matches the two o's in "food".

{n,}

nis a nonnegative integer. Matches at leastntimes. For example, 'o{2,}' does not match the "o" in "Bob" and matches all the o's in "foooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'.

{n,m}

Mandnare nonnegative integers, wheren<=m. Matches at leastnand at mostmtimes. For example, "o{1,3}" matches the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o?'. Note that you cannot put a space between the comma and the numbers.

?

When this character immediately follows any of the other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible. For example, in the string "oooo", 'o+?' matches a single "o", while 'o+' matches all 'o's.

.

Matches any single character except "\n". To match any character including the '\n', use a pattern such as '[\s\S]'.

(pattern)

A subexpression that matchespatternand captures the match. The captured match can be retrieved from the resulting Matches collection using the$0$9properties. To match parentheses characters ( ), use '\(' or '\)'.

(?:pattern)

A subexpression that matchespatternbut does not capture the match, that is, it is a non-capturing match that is not stored for possible later use. This is useful for combining parts of a pattern with the "or" character (|). For example, 'industr(?:y|ies) is a more economical expression than 'industry|industries'.

(?=pattern)

A subexpression that performs a positive lookahead search, which matches the string at any point where a string matchingpatternbegins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'Windows (?=95|98|NT|2000)' matches "Windows" in "Windows 2000" but not "Windows" in "Windows 3.1". Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.

(?!pattern)

A subexpression that performs a negative lookahead search, which matches the search string at any point where a string not matchingpatternbegins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'Windows (?!95|98|NT|2000)' matches "Windows" in "Windows 3.1" but does not match "Windows" in "Windows 2000". Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.

x|y

Matches eitherxory. For example, 'z|food' matches "z" or "food". '(z|f)ood' matches "zood" or "food".

[xyz]

A character set. Matches any one of the enclosed characters. For example, '[abc]' matches the 'a' in "plain".

[^xyz]

A negative character set. Matches any character not enclosed. For example, '[^abc]' matches the 'p' in "plain".

[a-z]

A range of characters. Matches any character in the specified range. For example, '[a-z]' matches any lowercase alphabetic character in the range 'a' through 'z'.

[^a-z]

A negative range characters. Matches any character not in the specified range. For example, '[^a-z]' matches any character not in the range 'a' through 'z'.

\b

Matches a word boundary, that is, the position between a word and a space. For example, 'er\b' matches the 'er' in "never" but not the 'er' in "verb".

\B

Matches a nonword boundary. 'er\B' matches the 'er' in "verb" but not the 'er' in "never".

\cx

Matches the control character indicated byx. For example, \cM matches a Control-M or carriage return character. The value ofxmust be in the range of A-Z or a-z. If not, c is assumed to be a literal 'c' character.

\d

Matches a digit character. Equivalent to [0-9].

\D

Matches a nondigit character. Equivalent to [^0-9].

\f

Matches a form-feed character. Equivalent to \x0c and \cL.

\n

Matches a newline character. Equivalent to \x0a and \cJ.

\r

Matches a carriage return character. Equivalent to \x0d and \cM.

\s

Matches any white space character including space, tab, form-feed, and so on. Equivalent to [ \f\n\r\t\v].


TAG:

 

评分:0

我来说两句

Open Toolbar