Linux Bash文本操作之grep篇

Linux grep命令用于查找文件里符合条件的字符串。是文本检索中常用的工具之一。

 grep  指令在文件中查找能够匹配指定模式字符串的行。如果没有指定文件名,或者文件名为  -  ,则从标准输入设备获取数据。默认会输出匹配行。

grep will print lines matching a pattern.
grep searches the named input FILEs for lines containing a match to the given PATTERN. If no files are specified, or “-” is given, grep searches standard input. 
By default, grep prints the matching lines.

实验文本获取方式见sed篇其一

使用方式如下

grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN]...  [-f FILE]...  [FILE...]

基础用法匹配文本内容

示例1查看文本中匹配   sed   字符的行。

示例2表示如果我们想包含大小写,使用大小写不敏感选项,查找内容就会包含  [sS][eE][dD]  。

示例3展示了还可以进行反向查找,也就是查找并打印不符合条件的行。

示例4和5用于输出匹配内容或不匹配内容的行的数量。

-G, --basic-regexp     Interpret PATTERN as a basic regular expression. This is the default.

-c, --count     Suppress normal output; instead print a count of matching lines for each input file.

-e PATTERN, --regexp=PATTERN     Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f, search for all patterns given.
                    This option can be used to protect a pattern beginning with “-”.

-f FILE, --file=FILE     Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e, search for all patterns given.
               The empty file contains zero patterns, and therefore matches nothing.

-i, --ignore-case    Ignore case distinctions in both the PATTERN and the input files.

-n, --line-number     Prefix each line of output with the 1-based line number within its input file.

-o, --only-matching   Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-v, --invert-match   Invert the sense of matching, to select non-matching lines.

-w, --word-regexp     Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line,
             or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character.
             Word-constituent characters are letters, digits, and the underscore.
cv@cv:~/myfiles$ grep 'sed' test.txt        #example-1
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ grep -i 'sed' test.txt     #example-2
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ grep -v 'sed' test.txt     #example-3
NAME
SYNOPSIS
DESCRIPTION
       in a pipeline which particularly distinguishes it from other types of editors.

       -n, --quiet, --silent
              suppress automatic printing of pattern space
       -e script, --expression=script
              add the script to the commands to be executed
       -f script-file, --file=script-file
              add the contents of script-file to the commands to be executed
       --follow-symlinks
              follow symlinks when processing in place
       -i[SUFFIX], --in-place[=SUFFIX]
cv@cv:~/myfiles$ grep -c 'sed' test.txt        #example-4
4
cv@cv:~/myfiles$ grep -c -v 'sed' test.txt     #example-5
14

示例6表示只匹配完整的单词而不包括子串的形式。

示例7只显示匹配到的地方,其他部分忽略掉,当需要把匹配变量存入变量时比较有用。

示例8打印匹配行的同时显示匹配行的行号。

示例9显示以  sed  开头的行,测试文本中没有这样的行,因此无输出。

示例10显示以  sed  开头的匹配,这样可以排除  used  这样开头不符合的字符串,与  \bsed  效果相同。

示例11显示以  sed  结尾的匹配,包含  used  ,因为其最后的子串确实为  sed  。

示例12和13都用来显示完全匹配  sed  的字符串,与上面的  -w  指令结果相同。

示例14可以用于匹配多个不同的字符串模式,如下面的示例所示,加上第二个指令后会将原文第六行的  Sed  也显示出来,否则匹配不到,因为首字母为大写。

cv@cv:~/myfiles$ grep -w 'sed' test.txt     #example-6
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ grep -o 'sed' test.txt     #example-7
sed
sed
sed
sed
sed

cv@cv:~/myfiles$ grep -n 'sed' test.txt     #example-8
2:       sed - stream editor for filtering and transforming text
4:       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
6:       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
7:       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ grep "^sed" test.txt         #example-9

cv@cv:~/myfiles$ grep '\<sed' test.txt         #example-10
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ grep 'sed\b' test.txt         #example-11
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ grep '\bsed\b' test.txt     #example-12
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text


cv@cv:~/myfiles$ grep '\bsed\>' test.txt     #example-13
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ grep -e 'sed' -e 'Sed' test.txt     #example-14
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

示例15从给定模式文件中或去匹配模式,每行表示一个,结果一起显示出来。

pattern.txt
sed
Sed
scripted

cv@cv:~/myfiles$ grep -f pattern.txt test.txt     #example-15
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text
       -e script, --expression=script
              add the script to the commands to be executed
       -f script-file, --file=script-file
              add the contents of script-file to the commands to be executed

 --color  的设置是为了将检索内容以彩色显示出来。

 A B C  的作用是同时打印匹配行的下NUM行。同时打印匹配行的上NUM行。同时打印匹配行的上下各NUM行。

示例1是使输出匹配项为彩色。

示例2是同时输出匹配行的后两行,3是前两行,4是前后各两行。

示例5是正则表达式中  |  的使用,在常规  grep  中应该使用反斜杠转义。

--color[=WHEN]        Surround the matched(non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets,
               and separators (for fields and groups of context  lines) with  escape sequences to display them in color on the
               terminal. The colors are defined by the environment variable GREP_COLORS. The deprecated environment variable
               GREP_COLOR is still supported, but its setting does not have priority. WHEN is never, always, or auto.

-A NUM, --after-context=NUM     Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous groups of matches.
-B NUM, --before-context=NUM    Print NUM lines of leading context before matching lines. Places a line containing a group separator (--) between contiguous groups of matches.
-C NUM, -NUM, --context=NUM     Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches.
For all the three command, with the -o or --only-matching option, this has no effect and a warning is given.
cv@cv:~/myfiles$ grep --color=auto sed  test.txt     #example-1
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ grep -A2 'Sed' test.txt             #example-2
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text
       in a pipeline which particularly distinguishes it from other types of editors.
cv@cv:~/myfiles$ grep -B2 'Sed' test.txt             #example-3
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
cv@cv:~/myfiles$ grep -C2 'Sed' test.txt             #example-4
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text
       in a pipeline which particularly distinguishes it from other types of editors.

cv@cv:~/myfiles$ grep 'sed\|Sed' test.txt             #example-5
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

文件名检索

对  grep  而言更实用的部分是对文件的检索。

 -l  的作用是使检索结果只保留文件名,而过滤掉在文件中匹配的位置,便于查看相关文件。

 -r  的意思就是递归检索,遇到文件夹就往文件夹下一级接着检索,直到独立的文件为止。

 -d  的作用是设置该命令检索到文件时的处理方式,当设置为 recurse 时,与直接使用  -r  效果相同。

示例1打印显示该目录下包含  sbin  的所有文件。

示例2使用  -d  实现了相同的结果,但是没有直接使用-r简洁。

示例3表示在检索过程中排除某个指定文件夹。

示例4表示排除多个文件夹时的写法。

当然我们也可以将多个欲排除在检索之列的文件名写入一个文件中,然后使用 grep -lr --exclude-from=FILE 'sbin' /usr/include/* 的方式排除这些文件。

示例5展示了反选的结果,也即是该目录下所有不包含给定模式字符串的文件的文件名。

示例6展示了利用文件名进一步过滤搜索结果的方式。

-l, --files-with-matches     Suppress normal output; instead print the name of each input file from which output would normally have been
                    printed. The scanning will stop on the first match.
-L, --files-without-match    Suppress normal output; instead print the name of each input file from which no output would normally have been printed.
                  The scanning will stop on the first match.

-r, --recursive     Read all files under each directory, recursively, following symbolic links only if they are on the command line.
              Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option.

-d ACTION, --directories=ACTION      Use ACTION to process directory.
                        By default, ACTION is read, just as if the directories were ordinary files.
                        If ACTION is skip, silently skip directories.
                        If ACTION is recurse, read all files under each directory, recursively, following symbolic links only if they are on the command line. Equivalent to -r.

--exclude-dir=DIR     Exclude directories matching the pattern DIR from recursive searches.
--exclude=GLOB        Skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and [...] as wildcards,
                     and \ to quote a wildcard or backslash character literally.
cv@cv:~/myfiles$ grep -lr 'sbin' /usr/include/*    #example-1
/usr/include/c++/5/streambuf
/usr/include/c++/5/bits/streambuf.tcc
/usr/include/c++/5/bits/ostream.tcc
/usr/include/mpi/openmpi/opal/mca/installdirs/installdirs.h
/usr/include/openmpi/openmpi/opal/mca/installdirs/installdirs.h
/usr/include/paths.h
/usr/include/rpcsvc/nislib.h

cv@cv:~/myfiles$ grep -l -d recurse 'sbin' /usr/include/*    #example-2
/usr/include/c++/5/streambuf
/usr/include/c++/5/bits/streambuf.tcc
/usr/include/c++/5/bits/ostream.tcc
/usr/include/mpi/openmpi/opal/mca/installdirs/installdirs.h
/usr/include/openmpi/openmpi/opal/mca/installdirs/installdirs.h
/usr/include/paths.h
/usr/include/rpcsvc/nislib.h

cv@cv:~/myfiles$ grep -lr --exclude-dir='mpi' 'sbin' /usr/include/*    #example-3
/usr/include/c++/5/streambuf
/usr/include/c++/5/bits/streambuf.tcc
/usr/include/c++/5/bits/ostream.tcc
/usr/include/openmpi/openmpi/opal/mca/installdirs/installdirs.h
/usr/include/paths.h
/usr/include/rpcsvc/nislib.h
cv@cv:~/myfiles$ grep -lr --exclude-dir={mpi,openmpi} 'sbin' /usr/include/*  #example-4 /usr/include/c++/5/streambuf /usr/include/c++/5/bits/streambuf.tcc /usr/include/c++/5/bits/ostream.tcc /usr/include/paths.h /usr/include/rpcsvc/nislib.h cv@cv:~/myfiles$ grep -Lr --exclude-dir={mpi,openmpi} 'sbin' /usr/include/* #example-5 /usr/include/aio.h /usr/include/aliases.h /usr/include/alloca.h /usr/include/alsa/pcm_plugin.h /usr/include/alsa/pcm.h ...

cv@cv:~/myfiles$ grep -lr --exclude=*.h 'sbin' /usr/include/
* #example-6
/usr/include/c++/5/streambuf
/usr/include/c++/5/bits/streambuf.tcc
/usr/include/c++/5/bits/ostream.tcc

当我们确切地知道需要检索的一整行内容时,可以使用  -x  排除掉其他无关干扰,只显示完全匹配的行。

就相当于在匹配模式字符串前面添加了  ^  后面添加了  $  一样。

使用  -F  可以将引号内的匹配模板当做一个固定字符串。

示例1这种直接匹配时,会将中间的小数点当做可以匹配任意一个字符的方式来工作。

示例2中,如果我们就是想匹配中间是小数点的文件内容,则需要使用反斜杠转义。

示例3表示还可以使用  -F  选项,将后面的匹配模式固定为一个字符串。

-x, --line-regexp     Select only those matches that exactly match the whole line.
               For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $.

-F, --fixed-strings   Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.
cv@cv:~/myfiles$ grep -r 'test.cpp' /usr/include/*    #example-1
/usr/include/boost/config/compiler/sunpro_cc.hpp:       //         while processing ../test.cpp at line 0.
/usr/include/boost/endian/detail/lightweight_test.hpp://  TODO: Should all test macros return bool?  See BOOST_TEST_MEM_EQ usage in fp_exaustive_test,cpp
/usr/include/boost/detail/named_template_params.hpp:      // iterator_adaptor_test.cpp when using this method.
/usr/include/boost/type_traits/is_convertible.hpp:// Enable this for your compiler if is_convertible_test.cpp will compile it...
/usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp:      // derived test-class (see test/boykov_kolmogorov_max_flow_test.cpp)
/usr/include/boost/math/special_functions/nonfinite_num_facets.hpp:// Change in nonfinite_num_facet.hpp Paul A. Bristow 11 Apr 11 makes legacy_test.cpp work OK.

cv@cv:~/myfiles$ grep -r 'test\.cpp' /usr/include/*    #example-2
/usr/include/boost/config/compiler/sunpro_cc.hpp:       //         while processing ../test.cpp at line 0.
/usr/include/boost/detail/named_template_params.hpp:      // iterator_adaptor_test.cpp when using this method.
/usr/include/boost/type_traits/is_convertible.hpp:// Enable this for your compiler if is_convertible_test.cpp will compile it...
/usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp:      // derived test-class (see test/boykov_kolmogorov_max_flow_test.cpp)
/usr/include/boost/math/special_functions/nonfinite_num_facets.hpp:// Change in nonfinite_num_facet.hpp Paul A. Bristow 11 Apr 11 makes legacy_test.cpp work OK.

cv@cv:~/myfiles$ grep -rF 'test.cpp' /usr/include/*    #example-3
/usr/include/boost/config/compiler/sunpro_cc.hpp:       //         while processing ../test.cpp at line 0.
/usr/include/boost/detail/named_template_params.hpp:      // iterator_adaptor_test.cpp when using this method.
/usr/include/boost/type_traits/is_convertible.hpp:// Enable this for your compiler if is_convertible_test.cpp will compile it...
/usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp:      // derived test-class (see test/boykov_kolmogorov_max_flow_test.cpp)
/usr/include/boost/math/special_functions/nonfinite_num_facets.hpp:// Change in nonfinite_num_facet.hpp Paul A. Bristow 11 Apr 11 makes legacy_test.cpp work OK.

当有多个文件待搜索时,默认显示匹配项的文件名。

当只有一个文件待搜索时,默认不显示文件名。

-H, --with-filename    Print the file name for each match. This is the default when there is more than one file to search.

-h, --no-filename     Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
cv@cv:~/myfiles$ grep -rF -Hn --exclude-dir={mpi,openmpi} 'sbin' /usr/include/*     #example-1
/usr/include/c++/5/streambuf:838:    __copy_streambufs_eof(basic_streambuf<char>* __sbin,
/usr/include/c++/5/streambuf:843:    __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
/usr/include/c++/5/bits/streambuf.tcc:116:    __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>* __sbin,
/usr/include/c++/5/bits/streambuf.tcc:122:      typename _Traits::int_type __c = __sbin->sgetc();
/usr/include/c++/5/bits/streambuf.tcc:132:        __c = __sbin->snextc();
/usr/include/c++/5/bits/streambuf.tcc:139:    __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
/usr/include/c++/5/bits/streambuf.tcc:143:      return __copy_streambufs_eof(__sbin, __sbout, __ineof);
/usr/include/c++/5/bits/ostream.tcc:120:    operator<<(__streambuf_type* __sbin)
/usr/include/c++/5/bits/ostream.tcc:124:      if (__cerb && __sbin)
/usr/include/c++/5/bits/ostream.tcc:128:              if (!__copy_streambufs(__sbin, this->rdbuf()))
/usr/include/c++/5/bits/ostream.tcc:139:      else if (!__sbin)
/usr/include/paths.h:39:        "/usr/bin:/bin:/usr/sbin:/sbin"
/usr/include/paths.h:59:#define _PATH_SENDMAIL  "/usr/sbin/sendmail"
/usr/include/rpcsvc/nislib.h:273:extern nis_error __nisbind_create (dir_binding *, const nis_server *,
/usr/include/rpcsvc/nislib.h:276:extern nis_error __nisbind_connect (dir_binding *) __THROW;
/usr/include/rpcsvc/nislib.h:277:extern nis_error __nisbind_next (dir_binding *) __THROW;
/usr/include/rpcsvc/nislib.h:278:extern void __nisbind_destroy (dir_binding *) __THROW;

cv@cv:~/myfiles$ grep -rF -hn --exclude-dir={mpi,openmpi} 'sbin' /usr/include/*     #example-2
838:    __copy_streambufs_eof(basic_streambuf<char>* __sbin,
843:    __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
116:    __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>* __sbin,
122:      typename _Traits::int_type __c = __sbin->sgetc();
132:      __c = __sbin->snextc();
139:    __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
143:      return __copy_streambufs_eof(__sbin, __sbout, __ineof);
120:    operator<<(__streambuf_type* __sbin)
124:      if (__cerb && __sbin)
128:          if (!__copy_streambufs(__sbin, this->rdbuf()))
139:      else if (!__sbin)
39:     "/usr/bin:/bin:/usr/sbin:/sbin"
59:#define      _PATH_SENDMAIL  "/usr/sbin/sendmail"
273:extern nis_error __nisbind_create (dir_binding *, const nis_server *,
276:extern nis_error __nisbind_connect (dir_binding *) __THROW;
277:extern nis_error __nisbind_next (dir_binding *) __THROW;
278:extern void __nisbind_destroy (dir_binding *) __THROW;

  -q  表示不输出任何信息,只要找到一个匹配就被认为是成功退出。

  -s  表示不输出关于文件不存在或文件不可读的错误信息。

-q, --quiet, --silent     Do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.

-s, --no-messages Suppress error messages about nonexistent or unreadable files.
cv@cv:~/myfiles$ grep -lr 'sbin' /usr/include/*    #example-1
/usr/include/c++/5/streambuf
/usr/include/c++/5/bits/streambuf.tcc
/usr/include/c++/5/bits/ostream.tcc
/usr/include/mpi/openmpi/opal/mca/installdirs/installdirs.h
/usr/include/openmpi/openmpi/opal/mca/installdirs/installdirs.h
/usr/include/paths.h
/usr/include/rpcsvc/nislib.h

cv@cv:~/myfiles$ grep -lrq 'sbin' /usr/include/*    #example-2

cv@cv:~/myfiles$ grep -l 'sbin' /usr/include/*      #example-3
grep: /usr/include/alsa: Is a directory
grep: /usr/include/arpa: Is a directory
grep: /usr/include/asm-generic: Is a directory
grep: /usr/include/c++: Is a directory
...
grep: /usr/include/pango-1.0: Is a directory
/usr/include/paths.h
grep: /usr/include/pixman-1: Is a directory
...

cv@cv:~/myfiles$ grep -ls 'sbin' /usr/include/*    #example-4
/usr/include/paths.h

其他示例。

示例1表示从  ifconfig  中提取网络  IP  地址

示例2和3用于显示所需进程的状态。

cv@cv:~/myfiles$ ifconfig enp6s0 | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'    #example-1
          inet addr:10.249.152.52  Bcast:10.249.159.255  Mask:255.255.248.0

cv@cv:~/myfiles$ ps aux | grep 'ssh'    #example-2
root      1645  0.0  0.0  65512  4964 ?        Ss   10月24   0:00 /usr/sbin/sshd -D
root     17684  0.0  0.0  94928  6988 ?        Ss   11:22   0:00 sshd: cv [priv]
cv       17780  0.0  0.0  94928  4476 ?        S    11:22   0:00 sshd: cv@notty
cv       17781  0.0  0.0  12880  1936 ?        Ss   11:22   0:00 /usr/lib/openssh/sftp-server
root     20338  0.0  0.0  94928  6608 ?        Ss   16:31   0:00 sshd: cv [priv]
root     20341  0.0  0.0  94928  6888 ?        Ss   16:31   0:00 sshd: cv [priv]
cv       20437  0.0  0.0  95236  4956 ?        S    16:31   0:01 sshd: cv@pts/8
cv       20470  0.0  0.0  94928  4344 ?        S    16:31   0:00 sshd: cv@notty
cv       20475  0.0  0.0  12880  2020 ?        Ss   16:31   0:00 /usr/lib/openssh/sftp-server
cv       21422  0.0  0.0  15960  1020 pts/8    R+   20:59   0:00 grep --color=auto ssh

cv@cv:~/myfiles$ ps aux | grep 'ssh' | grep -v 'grep'    #example-3
root      1645  0.0  0.0  65512  4964 ?        Ss   10月24   0:00 /usr/sbin/sshd -D
root     17684  0.0  0.0  94928  6988 ?        Ss   11:22   0:00 sshd: cv [priv]
cv       17780  0.0  0.0  94928  4476 ?        S    11:22   0:00 sshd: cv@notty
cv       17781  0.0  0.0  12880  1936 ?        Ss   11:22   0:00 /usr/lib/openssh/sftp-server
root     20338  0.0  0.0  94928  6608 ?        Ss   16:31   0:00 sshd: cv [priv]
root     20341  0.0  0.0  94928  6888 ?        Ss   16:31   0:00 sshd: cv [priv]
cv       20437  0.0  0.0  95236  4956 ?        S    16:31   0:01 sshd: cv@pts/8
cv       20470  0.0  0.0  94928  4344 ?        S    16:31   0:00 sshd: cv@notty
cv       20475  0.0  0.0  12880  2020 ?        Ss   16:31   0:00 /usr/lib/openssh/sftp-server

参考资料

[1] Linux grep 命令

[2] linux grep命令详解

[3] linux下的find文件查找命令与grep文件内容查找命令

猜你喜欢

转载自www.cnblogs.com/phillee/p/11820743.html