您的位置:首页 > 运维架构 > Shell

shell 中grep 和 awk的使用,与svn结合

2018-03-06 11:13 381 查看

github地址

shell 中grep 和 awk的使用,与svn结合(后续进行整理和添加)

为了提高在命令行下工作效率

eg:

svn add
svn st | grep ^? | awk '{print $2}'
;

svn delete
svn st | grep ^! | awk '{print $2}'
;

### grep 历史

grep这个应用程序最早由肯·汤普逊写成。grep原先是ed下的一个应用程序,名称来自于g/re/p(globally search a regular expression and print,以正规表示法进行全域查找以及打印)。在ed下,输入g/re/p这个命令后,会将所有匹配先定义样式的字符串,以行为单位打印出来。

在1973年,Unix第四版中,grep首次出现在man页面中。

man 手册中对于grep命令的介绍

NAME

grep, egrep, fgrep, zgrep, zegrep, zfgrep – file pattern searcher

SYNOPSIS

grep [-abcdDEFGHhIiJLlmnOopqRSsUVvwxZ] [-A num] [-B num] [-C[num]]

[-e pattern] [-f file] [–binary-files=value] [–color[=when]]

[–colour[=when]] [–context[=num]] [–label] [–line-buffered]

[–null] [pattern] [file …]

DESCRIPTION

The grep utility searches any given input files, selecting lines that

match one or more patterns. By default, a pattern matches an input line

if the regular expression (RE) in the pattern matches the input line

without its trailing newline. An empty expression matches every line.

Each input line that matches at least one of the patterns is written to

the standard output.

grep is used for simple patterns and basic regular expressions (BREs);
egrep can handle extended regular expressions (EREs).  See re_format(7)
for more information on regular expressions.  fgrep is quicker than both
grep and egrep, but can only handle fixed patterns (i.e. it does not
interpret regular expressions).  Patterns may consist of one or more
lines, allowing any of the pattern lines to match a portion of the input.

zgrep, zegrep, and zfgrep act like grep, egrep, and fgrep, respectively,
but accept input files compressed with the compress(1) or gzip(1) com-
pression utilities.

The following options are available:

-A num, --after-context=num
Print num lines of trailing context after each match.  See also
the -B and -C options.

-a, --text
Treat all files as ASCII text.  Normally grep will simply print
``Binary file ... matches'' if files contain binary characters.
Use of this option forces grep to output lines matching the spec-
ified pattern.

-B num, --before-context=num
Print num lines of leading context before each match.  See also
the -A and -C options.
-b, --byte-offset
The offset in bytes of a matched pattern is displayed in front of
the respective matched line.

-C[num, --context=num]
Print num lines of leading and trailing context surrounding each
match.  The default is 2 and is equivalent to -A 2 -B 2.  Note:
no whitespace may be given between the option and its argument.

-c, --count
Only a count of selected lines is written to standard output.

--colour=[when, --color=[when]]
Mark up the matching text with the expression stored in
GREP_COLOR environment variable.  The possible values of when can
be `never', `always' or `auto'.

-D action, --devices=action
Specify the demanded action for devices, FIFOs and sockets.  The
default action is `read', which means, that they are read as if
they were normal files.  If the action is set to `skip', devices
will be silently skipped.

-d action, --directories=action
Specify the demanded action for directories.  It is `read' by
default, which means that the directories are read in the same
manner as normal files.  Other possible values are `skip' to
silently ignore the directories, and `recurse' to read them
recursively, which has the same effect as the -R and -r option.

-E, --extended-regexp
Interpret pattern as an extended regular expression (i.e. force
grep to behave as egrep).

-e pattern, --regexp=pattern
Specify a pattern used during the search of the input: an input
line is selected if it matches any of the specified patterns.
This option is most useful when multiple -e options are used to
specify multiple patterns, or when a pattern begins with a dash
(`-').
--exclude
If specified, it excludes files matching the given filename pat-
tern from the search.  Note that --exclude patterns take priority
over --include patterns, and if no --include pattern is speci-
fied, all files are searched that are not excluded.  Patterns are
matched to the full path specified, not only to the filename com-
ponent.

--exclude-dir
If -R is specified, it excludes directories matching the given
filename pattern from the search.  Note that --exclude-dir pat-
terns take priority over --include-dir patterns, and if no
--include-dir pattern is specified, all directories are searched
that are not excluded.

-F, --fixed-strings
Interpret pattern as a set of fixed strings (i.e. force grep to
behave as fgrep).

-f file, --file=file
Read one or more newline separated patterns from file.  Empty
pattern lines match every input line.  Newlines are not consid-
ered part of a pattern.  If file is empty, nothing is matched.

-G, --basic-regexp
Interpret pattern as a basic regular expression (i.e. force grep
to behave as traditional grep).

-H      Always print filename headers with output lines.

-h, --no-filename
Never print filename headers (i.e. filenames) with output lines.

--help  Print a brief help message.

-I      Ignore binary files.  This option is equivalent to
--binary-file=without-match option.

-i, --ignore-case
Perform case insensitive matching.  By default, grep is case sen-
sitive.

--include
If specified, only files matching the given filename pattern are
searched.  Note that --exclude patterns take priority over
--include patterns.  Patterns are matched to the full path speci-
fied, not only to the filename component.


–include-dir

If -R is specified, only directories matching the given filename

pattern are searched. Note that –exclude-dir patterns take pri-

ority over –include-dir patterns.

-J, --bz2decompress
Decompress the bzip2(1) compressed file before looking for the
text.

-L, --files-without-match
Only the names of files not containing selected lines are written
to standard output.  Pathnames are listed once per file searched.
If the standard input is searched, the string ``(standard
input)'' is written.

-l, --files-with-matches
Only the names of files containing selected lines are written to
standard output.  grep will only search a file until a match has
been found, making searches potentially less expensive.  Path-
names are listed once per file searched.  If the standard input
is searched, the string ``(standard input)'' is written.

--mmap  Use mmap(2) instead of read(2) to read input, which can result in
better performance under some circumstances but can cause unde-
fined behaviour.

-m num, --max-count=num
Stop reading the file after num matches.

-n, --line-number
Each output line is preceded by its relative line number in the
file, starting at line 1.  The line number counter is reset for
each file processed.  This option is ignored if -c, -L, -l, or -q
is specified.

--null  Prints a zero-byte after the file name.

-O      If -R is specified, follow symbolic links only if they were
explicitly listed on the command line.  The default is not to
follow symbolic links.

-o, --only-matching
Prints only the matching part of the lines.

-p      If -R is specified, no symbolic links are followed.  This is the
default.


-q, –quiet, –silent

Quiet mode: suppress normal output. grep will only search a file

until a match has been found, making searches potentially less

expensive.

-R, -r, --recursive
Recursively search subdirectories listed.

-S      If -R is specified, all symbolic links are followed.  The default
is not to follow symbolic links.

-s, --no-messages
Silent mode.  Nonexistent and unreadable files are ignored (i.e.
their error messages are suppressed).

-U, --binary
Search binary files, but do not attempt to print them.

-V, --version
Display version information and exit.

-v, --invert-match
Selected lines are those not matching any of the specified pat-
terns.

-w, --word-regexp
The expression is searched for as a word (as if surrounded by
`[[:<:]]' and `[[:>:]]'; see re_format(7)).

-x, --line-regexp
Only input lines selected against an entire fixed string or regu-
lar expression are considered to be matching lines.

-y      Equivalent to -i.  Obsoleted.

-Z, -z, --decompress
Force grep to behave as zgrep.


–binary-files=value

Controls searching and printing of binary files. Options are

binary, the default: search binary files but do not print them;

without-match: do not search binary files; and text: treat all

files as text.

--context[=num]
Print num lines of leading and trailing context.  The default is
2.

--line-buffered
Force output to be line buffered.  By default, output is line
buffered when standard output is a terminal and block buffered
otherwise.

If no file arguments are specified, the standard input is used.


ENVIRONMENT

GREP_OPTIONS May be used to specify default options that will be placed

at the beginning of the argument list. Backslash-escaping

is not supported, unlike the behavior in GNU grep.

EXIT STATUS

The grep utility exits with one of the following values:

0     One or more lines were selected.
1     No lines were selected.
>1    An error occurred.


EXAMPLES

To find all occurrences of the word `patricia’ in a file:

$ grep 'patricia' myfile

To find all occurrences of the pattern `.Pp' at the beginning of a line:

$ grep '^\.Pp' myfile

The apostrophes ensure the entire expression is evaluated by grep instead
of by the user's shell.  The caret `^' matches the null string at the
beginning of a line, and the `\' escapes the `.', which would otherwise
match any character.

To find all lines in a file which do not contain the words `foo' or
`bar':

$ grep -v -e 'foo' -e 'bar' myfile

A simple example of an extended regular expression:

$ egrep '19|20|25' calendar

Peruses the file `calendar' looking for either 19, 20, or 25.


SEE ALSO

ed(1), ex(1), gzip(1), sed(1), re_format(7)

STANDARDS

The grep utility is compliant with the IEEE Std 1003.1-2008 (“POSIX.1”)

specification.

The flags [-AaBbCDdGHhIJLmoPRSUVwZ] are extensions to that specification,
and the behaviour of the -f flag when used with an empty pattern file is
left undefined.

All long options are provided for compatibility with GNU versions of this
utility.

Historic versions of the grep utility also supported the flags [-ruy].
This implementation supports those options; however, their use is
strongly discouraged.


HISTORY

The grep command first appeared in Version 6 AT&T UNIX.

BUGS

The grep utility does not normalize Unicode input, so a pattern contain-

ing composed characters will not match decomposed input, and vice versa.

### awk 简介

AWK是一种优良的文本处理工具,Linux及Unix环境中现有的功能最强大的数据处理引擎之一。这种编程及数据操作语言(其名称得自于它的创始人阿尔佛雷德·艾侯、彼得·温伯格和布莱恩·柯林汉姓氏的首个字母)的最大功能取决于一个人所拥有的知识。AWK提供了极其强大的功能:可以进行正则表达式的匹配,样式装入、流控制、数学运算符、进程控制语句甚至于内置的变量和函数。它具备了一个完整的语言所应具有的几乎所有精美特性。实际上AWK的确拥有自己的语言:AWK程序设计语言,三位创建者已将它正式定义为“样式扫描和处理语言”。它允许您创建简短的程序,这些程序读取输入文件、为数据排序、处理数据、对输入执行计算以及生成报表,还有无数其他的功能。gawk是AWK的GNU版本。

最简单地说,AWK是一种用于处理文本的编程语言工具。AWK在很多方面类似于Unix shell编程语言,尽管AWK具有完全属于其本身的语法。它的设计思想来源于SNOBOL4、sed、Marc Rochkind设计的有效性语言、语言工具yacc和lex,当然还从C语言中获取了一些优秀的思想。在最初创造AWK时,其目的是用于文本处理,并且这种语言的基础是,只要在输入数据中有模式匹配,就执行一系列指令。该实用工具扫描文件中的每一行,查找与命令行中所给定内容相匹配的模式。如果发现匹配内容,则进行下一个编程步骤。如果找不到匹配内容,则继续处理下一行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell grep awk