Chinaunix首页 | 论坛 | 博客
  • 博客访问: 248138
  • 博文数量: 91
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-12 09:38
文章分类

全部博文(91)

文章存档

2017年(1)

2011年(1)

2008年(15)

2007年(74)

我的朋友

分类: LINUX

2007-08-19 15:05:16

grep
   search file or standard input for lines containing a match to regular expression regex. by default,matching lines will be displayed and nonmatching line will not be displayed. when multiple files are specified, grep displays the filename as a prefix to the output lines( use the -h to suppress filename prefixes)
   -c display only a count of matched lines,but not the lines themselves
   -h display matched lines.but don't include filename for multiple file input
   -i ignore uppercase and lowercase distinction. allowing abc to match both abc and ABC.
   -n display matches lines prefixed with their line numbers. when used with multiple files.both the filename and line number are prefixed
   -v print all lined do not match regexs. this is an important and useful option.you want to use regular expression, not only to select information but also to elimilate information. using -v inverts(颠倒,翻转) the output this way.
   -o, --only-matching Show only the part of a matching line that matches PATTERN.
   -R, -r, --recursive Read all files under each directory, recursively; this is equivalent to the -d recurse option.
                         regular expression
                        匹配文件名的通配符
   * 匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 \*。
   ? 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?。
   [abc] 字符集合。匹配所包含的任意一个字符。例如, '
[abc]' 可以匹配 "plain" 中的 'a'。
   [!abc] 负值字符集合。匹配未包含的任意字符。例如, '
[^abc]' 可以匹配 "plain" 中的'p'。
   [a-z] 字符范围。匹配指定范围内的任意字符。例如,'
[a-z]' 可以匹配 'a' 到 'z' 范围内的任意小写字母字符。
   [!a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如,'
[^a-z]' 可以匹配任何不在 'a' 到 'z' 范围内的任意字符
   {flag1,fiag2,flag3....} crate strings flag1,flag2,flag3,etc.for example,file_{one,two,three} yields the strings file_one,file_two,file_three.this is a special operator named brace expansion.that can be used to match filenames but isn'
t examine directories for existing files to match.instead,it will expand any string.for example,it can be used with echo to yield strings totally unrelated to existing filenames
   echo string_{a,b,c} string_a,string_b,string_c
   example:
   find /home file[a-z1-9]
   find /home file[!a-o1-8]
   find /home file?
   find /home file*
   less file{1,2,3}
                   匹配文件内容的通配符
* 匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 \*
   + 匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 \+。must have option -E
   . 匹配除换行符 \n之外的任何单字符。要匹配 .,请使用 \。
   ? 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?。must have the option -E. otherwise have no result.
   [ 匹配输入字符串的开始位置,除非在方括号表达式中使用,此时它表示不接受该字符集合。要匹配 ^ 字符本身,请使用 \^。
   { 标记限定符表达式的开始。要匹配 {,请使用 \{。
   | 指明两项之间的一个选择。要匹配 |,请使用 \|
   \{n,m\} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o\{1,3\}" 将匹配 "fooooood" 中的前三个 o。'o\{0,1\}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。
   \{n,\} n 是一个非负整数。至少匹配n 次。例如,'o\{2,\}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+''o{0,}' 则等价于 'o*',enu[0-9][0-9]等价于 enu[0-9]\{2\}.
   \{n\} n 是一个非负整数。匹配确定的 n 次。例如,'o\{2\}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
   + 匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"+ 等价于 \{1,\}
   ^ 匹配输入字符串的开始位置。
   $ 匹配输入字符串的结束位置。
   [abc] 字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'
   [^abc] 负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'
   [a-z] 字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配 'a''z' 范围内的任意小写字母字符。
   [^a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如,'[^a-z]' 可以匹配任何不在 'a''z' 范围内的任意字符
   \d 匹配一个数字字符。等价于 [0-9]
   \D 匹配一个非数字字符。等价于 [^0-9]
   () 标记一个子表达式的开始和结束位置。
   echo string_{a,b,c} string_a,string_b,string_c
   for example:ls con*/*/inv*.txt
   \<word\> match word. the backslash are required and enable this interpretation of < and >
   | alternation(交替,轮流). match either the regex specified before or after the vertical bar. this modifier is an modifier is an "extended" feature and available in grep only when the -E command-line option is used
   ? match zero or one instance of the precedeing regex. this modifier is an "extended" feature and available in grep only when the -E command-line option is used.
   + match one or more instances of the preceding regex. this modifier is an "extended" feature and available in grep only when the -E command-line option is used
   for example
   display all line from file1 contain '111'.'1111' or '11111' on a line by itself:
   grep '^1\{3,5\}$' file1
   display all line from file1 contain "happy", "Happy", "sad", "Sad", "Angry" or "angry":
   grep -E '[Hh]appy|[Ss]ad|[Aa]ngry' file1
   display all lines from file1 that contain two or more adjacent(邻近的,毗邻的) digits:
   grep '[0-9][0-9][0-9]*' file1
   display all lines from file1 where the string "linux" appears at the start of the line
    grep '^linux' file1
   display lines in file1 where the last character is an 'x':
    prep 'x$' file1
   display the number of empty lines in file1 with nothig between the begin and the end:
    grep -c '^$' file1
   display all lines from file1 containing only the world "null" by itself
    grep '^null$' file1
   display all lines from file1 that beginning with any single character other than a digit:
    grep '^[^0-9]' file1
   display all nonblank lines from file1
    grep '.' file1
   display all line from file1 with three or more characters on a line(exclude the newline character)
    grep '...' file1
   display all line from file that contain "file"(because ?(question mark) can match zero more occurrences), file1 or file2:
    grep -E 'file[12]?' file1
   display all lines from file1 that contain at least a digit
    grep -E '[0-9]+' file1

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