ITPub博客

首页 > Linux操作系统 > Linux操作系统 > grep的一些例子

grep的一些例子

原创 Linux操作系统 作者:hjgluguo 时间:2010-01-20 17:58:27 0 删除 编辑

Option What It Does
 
–b Precedes each line by the block number on which it was found. This is sometimes useful in locating disk block numbers by context.
 
–c Displays a count of matching lines rather than displaying the lines that match.
 
–h Does not display filenames.
 
–i Ignores the case of letters in comparisons (i.e., upper- and lowercase are considered identical).
 
–l Lists only the names of files with matching lines (once), separated by newline characters.
 
–n Precedes each line by its relative line number in the file.
 
–s Works silently, that is, displays nothing except error messages. This is useful for checking the exit status.
 
–v Inverts the search to display only lines that do not match.
 
–w Searches for the expression as a word, as if surrounded by \< and \>. This applies to grep only. (Not all versions of grep support this feature; e.g., SCO UNIX does not.)
 

 

[oracle@redhat4 test_crontab]$ grep 'oracle:' /etc/passwd
oracle:x:501:501::/home/oracle:/bin/bash
[oracle@redhat4 test_crontab]$ echo $?
0
[oracle@redhat4 test_crontab]$ grep 'oracleas:' /etc/passwd
[oracle@redhat4 test_crontab]$ echo $?
1
[oracle@redhat4 test_crontab]$ grep 'oracle:' /etc/passwdsdf
grep: /etc/passwdsdf: No such file or directory
[oracle@redhat4 test_crontab]$ echo $?
2


[oracle@redhat4 test_crontab]$ cat testlogname
northwest       NW      Charles Main            3.0     .98     3       34
western         WE      Sharon Gray             5.3     .97     5       23
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13
oracle          ES
$logname                AE
[oracle@redhat4 test_crontab]$ grep -i "$LOGNAME" testlogname
oracle          ES
[oracle@redhat4 test_crontab]$ grep -i '$LOGNAME' testlogname
$logname                AE

EXPLANATION

The value of the shell ENV variable, LOGNAME, is printed. It contains the user's login name. If the variable is enclosed in double quotes, it will still be expanded by the shell, and in case there is more than one word assigned to the variable, whitespace is shielded from shell interpretation. If single quotes are used, variable substitution does not take place; that is, $LOGNAME is printed.

 

Table 4.9. The Bracketed Character Class Bracket Class
 Meaning
 
[:alnum:] Alphanumeric characters
 
[:alpha:] Alphabetic characters
 
[:cntrl:] Control characters
 
[:digit:] Numeric characters
 
[:graph:] Nonblank characters (not spaces, control characters, etc.)
 
[:lower:] Lowercase letters
 
[:print:] Like [:graph:], but includes the space character
 
[:punct:] Punctuation characters
 
[:space:] All whitespace characters (newlines, spaces, tabs)
 
[:upper:] Uppercase letters
 
[:xdigit:] Allows digits in a hexadecimal number (0-9a-fA-F)
 
[oracle@redhat4 test_crontab]$ grep '[[:space:]]\.[[:digit:]][[:space:]]' datafile
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southeast       SE      Patricia Hemenway       4.0     .7      4       17
[oracle@redhat4 test_crontab]$ grep -E '[[:space:]]\.[[:digit:]][[:space:]]' datafile
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southeast       SE      Patricia Hemenway       4.0     .7      4       17
[oracle@redhat4 test_crontab]$ egrep '[[:space:]]\.[[:digit:]][[:space:]]' datafile
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southeast       SE      Patricia Hemenway       4.0     .7      4       17


[oracle@redhat4 test_crontab]$  egrep 'NW|EA' datafile
northwest       NW      Charles Main            3.0     .98     3       34
eastern         EA      TB Savage               4.4     .84     5       20
[oracle@redhat4 test_crontab]$ grep -E 'NW|EA' datafile
northwest       NW      Charles Main            3.0     .98     3       34
eastern         EA      TB Savage               4.4     .84     5       20
[oracle@redhat4 test_crontab]$ grep 'NW|EA' datafile
[oracle@redhat4 test_crontab]$ grep 'NW\|EA' datafile
northwest       NW      Charles Main            3.0     .98     3       34
eastern         EA      TB Savage               4.4     .84     5       20


[oracle@redhat4 test_crontab]$ grep '' datafile
[oracle@redhat4 test_crontab]$ grep '\' datafile
north           NO      Margot Weber            4.5     .89     5       9
[oracle@redhat4 test_crontab]$ grep -E '\' datafile
north           NO      Margot Weber            4.5     .89     5       9
[oracle@redhat4 test_crontab]$  egrep '\' datafile
north           NO      Margot Weber            4.5     .89     5       9

 

[oracle@redhat4 test_crontab]$ grep 'w(es)t.*\1' datafile
grep: Invalid back reference
[oracle@redhat4 test_crontab]$ grep 'w\(es\)t.*\1' datafile
northwest       NW      Charles Main            3.0     .98     3       34
[oracle@redhat4 test_crontab]$ grep -E 'w(es)t.*\1' datafile
northwest       NW      Charles Main            3.0     .98     3       34
[oracle@redhat4 test_crontab]$ egrep 'w(es)t.*\1' datafile
northwest       NW      Charles Main            3.0     .98     3       34

 

[oracle@redhat4 test_crontab]$ grep -c 'west' datafile
3

EXPLANATION

The –c option causes grep to print the number of lines where the pattern was found. This does not mean the number of occurrences of the pattern. For example, if west is found three times on a line, it only counts the line once.


[oracle@redhat4 test_crontab]$ grep -w 'north' datafile
north           NO      Margot Weber            4.5     .89     5       9

EXPLANATION

The –w option causes grep to find the pattern only if it is a word,[a] not part of a word. Only the line containing the word north is printed, not northwest, northeast, etc.


[oracle@redhat4 test_crontab]$ grep -2 Patricia datafile
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13

EXPLANATION

After a line matching Patricia is found, grep displays that line and the two lines before and after it.

The –C option is the same as –2. But -C Can't use in Red Hat Linux 4.7


[oracle@redhat4 test_crontab]$ grep -A 2 Patricia datafile
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13

EXPLANATION

After a line matching Patricia is found, grep displays that line and the two lines after it.


[oracle@redhat4 test_crontab]$ grep -B 2 Patricia datafile
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17

EXPLANATION

After a line matching Patricia is found, grep displays that line and the two lines before (preceding) it.

 


[oracle@redhat4 test_crontab]$ grep -b '[abc]' datafile
0:northwest     NW      Charles Main            3.0     .98     3       34
41:western              WE      Sharon Gray             5.3     .97     5       23
80:southwest    SW      Lewis Dalsass           2.7     .8      2       18
120:southern    SO      Suan Chin               5.1     .95     4       15
156:southeast   SE      Patricia Hemenway       4.0     .7      4       17
199:eastern             EA      TB Savage               4.4     .84     5       20
235:northeast   NE      AM Main Jr.             5.1     .94     3       13
274:north               NO      Margot Weber            4.5     .89     5       9
310:central             CT      Ann Stephens            5.7     .94     5       13

 

[oracle@redhat4 test_crontab]$ cat repatterns
western
north
[oracle@redhat4 test_crontab]$ grep -f repatterns datafile
northwest       NW      Charles Main            3.0     .98     3       34
western         WE      Sharon Gray             5.3     .97     5       23
northeast       NE      AM Main Jr.             5.1     .94     3       13
north           NO      Margot Weber            4.5     .89     5       9

EXPLANATION
With the –f option followed by a filename (in this example, repatterns), grep will get its search patterns from that file and match them against lines in datafile. Grep searched for and printed all lines containing patterns western and north.


[oracle@redhat4 test_crontab]$ cat db
123
abc
[oracle@redhat4 test_crontab]$ grep '[0-9]' datafile db
datafile:northwest      NW      Charles Main            3.0     .98     3       34
datafile:western                WE      Sharon Gray             5.3     .97     5       23
datafile:southwest      SW      Lewis Dalsass           2.7     .8      2       18
datafile:southern       SO      Suan Chin               5.1     .95     4       15
datafile:southeast      SE      Patricia Hemenway       4.0     .7      4       17
datafile:eastern                EA      TB Savage               4.4     .84     5       20
datafile:northeast      NE      AM Main Jr.             5.1     .94     3       13
datafile:north          NO      Margot Weber            4.5     .89     5       9
datafile:central                CT      Ann Stephens            5.7     .94     5       13


[oracle@redhat4 test_crontab]$ grep -h '[0-9]' datafile db
northwest       NW      Charles Main            3.0     .98     3       34
western         WE      Sharon Gray             5.3     .97     5       23
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13
123

EXPLANATION

If more than one file is listed, grep prepends each line of its output with the filename. Filenames are datafile and db.

With the –h option, grep suppresses the header information; i.e., does not print the filenames.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7865774/viewspace-625618/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 常用的分析函数
请登录后发表评论 登录
全部评论

注册时间:2008-07-11

  • 博文量
    97
  • 访问量
    182050