linux命令学习笔记之grep

发表于: lnmp | 作者: | 日期: 2009/7/16 09:07
标签: ,

命令简介
grep是linux/unix系统下一个基于正则表达式的的文本搜索工具,该命令强大且复杂。不过目前我对该命令的使用也仅仅限于配合ps或者ls来查看一下进程或者文件列表而已。

常用命令组合:

ps -ef |grep java 或者 ps -ef |grep ‘java’

查看CMD一项中包含java字符串的进程。有没有引号没有影响。

点击此处,详细了解一下ps命令。

在某目录下,有如下几个文件:

nas3_old:bossweb:/bossweb/mixh/zengtest>ls -l
total 72
drwxr-xr-x 2 bossweb boss 256 Jul 15 21:55 ./
drwxr-x— 15 bossweb boss 4096 Jul 15 21:21 ../
-rw-r—– 1 bossweb boss 34 Jul 15 22:00 a.txt
-rw-r–r– 1 bossweb boss 0 Jul 15 21:55 ab.txt
-rw-r–r– 1 bossweb boss 0 Jul 15 21:55 b.txt
-rw-r—– 1 bossweb boss 27810 Apr 11 15:50 test.jsp

通过管道过滤ls显示的内容,只显示以字母d开头的输出:

nas3_old:bossweb:/bossweb/mixh/zengtest>ls -l |grep ‘^d’
drwxr-xr-x 2 bossweb boss 256 Jul 15 21:55 ./
drwxr-x— 15 bossweb boss 4096 Jul 15 21:21 ../

通过管道过滤ls显示的内容,只显示以txt文本文件列表:

nas3_old:bossweb:/bossweb/mixh/zengtest>ls -l |grep ‘^d’
nas3_old:bossweb:/bossweb/mixh/zengtest>ls -l |grep ‘txt$’
-rw-r—– 1 bossweb boss 34 Jul 15 22:00 a.txt
-rw-r–r– 1 bossweb boss 0 Jul 15 21:55 ab.txt
-rw-r–r– 1 bossweb boss 0 Jul 15 21:55 b.txt


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

以下内容来自AIX手册:

grep Command

Purpose

Syntax

grep [ -E | -F ] [ -i ] [ -h ] [ -H ] [ -L ] [ -r | -R ] [ -s ] [ -v ] [ -w ] [ -x ] [ -y ] [ [ [ -b ] [ -n ] ] | [ -c | -l
| -q ] ] [ -p [ Separator ] ] { [ -e PatternList … ] [ -f PatternFile … ] | PatternList … } [ File … ]

Description

The grep command searches for the pattern specified by the Pattern parameter and writes each matching line to standard
output. The patterns are limited regular expressions in the style of the ed or egrep command. The grep command uses a
compact non-deterministic algorithm.

The grep command displays the name of the file containing the matched line if you specify more than one name in the File
parameter. Characters with special meaning to the shell ($, *, [, |, ^, (, ), \ ) must be in quotation marks when they
appear in the Pattern parameter. When the Pattern parameter is not a simple string, you usually must enclose the entire
pattern in single quotation marks. In an expression such as [a-z], the – (minus sign) cml specifies a range, according to
the current collating sequence. A collating sequence may define equivalence classes for use in character ranges. If no
files are specified, grep assumes standard input.

Notes:
1 Do not run the grep command on a special file because it produces unpredictable results.
2 Input lines should not contain the NULL character.
3 Input files should end with the newline character.
4 The newline character will not be matched by the regular expressions.
5 Although some flags can be specified simultaneously, some flags override others. For example, the -l option takes
precedence over all other flags. And if you specify both the -E and -F flags, the last one specified takes priority.

Flags

-b
Precedes each line by the block number on which it was found. Use this flag to help find disk block numbers by
context. The -b flag cannot be used with input from stdin or pipes.
-c
Displays only a count of matching lines.
-E
Treats each pattern specified as an extended regular expression (ERE). A NULL value for the ERE matches every line.

Note: The grep command with the -E flag is the same as the egrep command, except that error and usage messages are
different and the -s flag functions differently.

-e PatternList
Specifies one or more search patterns. This works like a simple pattern but is useful when the pattern begins with a –
(minus). Patterns should be separated by a new-line character. A NULL pattern can be specified by two adjacent new-
line characters or a quotation mark followed by a new-line character (“\n). Each pattern is treated like a basic
regular expression (BRE) unless the -E or -F flag is also specified. Multiple -e and -f flags are accepted by grep.
All of the specified patterns are used when matching lines, but the order of evaluation is unspecified.
-F
Treats each specified pattern as a string instead of a regular expression. A NULL string matches every line.

Note: The grep command with the -F flag is the same as the fgrep command, except that error and usage messages are
different and the -s flag functions differently.

-f PatternFile
Specifies a file containing search patterns. Each pattern should be separated by a new-line character, and an empty
line is considered a NULL pattern. Each pattern is treated like a basic regular expression (BRE), unless the -E or -F
flag is also specified.
-h
Prevents the name of the file containing the matching line from being appended to that line. Suppresses file names
when multiple files are specified.
-H
If the -r or -R option is specified and a symbolic link referencing a file of type directory is specified on the
command line, grep will search the files of the directory referenced by the symbolic link and all the files in the
file hierarchy below it.
-i
Ignores the case (uppercase or lowercase) of letters when making comparisons.

-l
Lists just the names of files (once) which contain matching lines. Each file name is separated by a new-line
character. If standard input is searched, a path name of (StandardInput) is returned. The -l flag with any combination
of the -c and -n flags behaves like the -l flag only.
-L
If the -r or -R option is specified and a symbolic link referencing a file of type directory is specified on the
command line or encountered during the traversal of a file hierarchy, grep shall search the files of the directory
referenced by the symbolic link and all the files in the file hierarchy below it. If both -H and -L are specified, the
last option specified on the command line takes effect.
-n
Precedes each line with the relative line number in the file. Each file starts at line 1, and the line counter is
reset for each file processed.
-p[Separator]
Displays the entire paragraph containing matched lines. Paragraphs are delimited by paragraph separators, as specified
by the Separator parameter, which are patterns in the same form as the search pattern. Lines containing the paragraph
separators are used only as separators; they are never included in the output. The default paragraph separator is a
blank line.
-q
Suppresses all writing to standard output, regardless of matching lines. Exits with a zero status if an input line is
selected. The -q flag with any combination of the -c, -l and -n flags behaves like the -q flag only.
-r
Searches directories recursively. By default, links to directories are followed.
-R
Searches directories recursively. By default, links to directories are not followed.
-s
Suppresses error messages ordinarily written for nonexistent or unreadable files. Other error messages are not
suppressed.
-v
Displays all lines not matching the specified pattern.
-w
Does a word search.
-x
Displays lines that match the specified pattern exactly with no additional characters.
-y
Ignores the case of letters when making comparisons.
PatternList
Specifies one or more patterns to be used during the search. The patterns are treated as if they were specified using
the -e flag.
File
Specifies a name of a file to be searched for patterns. If no File variable is given, the standard input is used.

Exit Status

This command returns the following exit values:

0
A match was found.
1
No match was found.
>1
A syntax error was found or a file was inaccessible (even if matches were found).

Examples
1 To use a pattern that contains some of the pattern-matching characters *, ^, ?, [, ], \(, \), \{, and \}, enter:

grep “^[a-zA-Z]” pgm.s

This displays every line in pgm.s whose first character is a letter.
2 To display all lines that do not match a pattern, enter: grep -v “^#” pgm.s

This displays every line in pgm.s whose first character is not a # (pound sign).
3 To display all lines in the file1 file that match either the abc or xyz string, enter: grep -E “abc|xyz” file1

4 To search for a $ (dollar sign) in the file named test2, enter:

grep \\$ test2

The \\ (double backslash) characters are necessary in order to force the shell to pass a \$ (single backslash, dollar
sign) to the grep command. The \ (single backslash) character tells the grep command to treat the following character
(in this example the $) as a literal character rather than an expression character. Use the fgrep command to avoid the
necessity of using escape characters such as the backslash.
5 To search recursively through /tmp to find files which have the word IBM without recursing through links pointing to
directories, type:

grep -R IBM /tmp
OR

grep -r -H IBM /tmp
6 To search recursively through /tmp to find files which have the word IBM and recurse through links as well, type:

grep -r IBM /tmp
OR

grep -R -L IBM /tmp

Files

/usr/bin/grep
Contains the grep command.

参考链接:
http://man.chinaunix.net/newsoft/grep/open.htm

: https://blog.darkmi.com/2009/07/16/903.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.