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 -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-
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-
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-
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- cv@cv:~/myfiles$ grep -c -v 'sed' test.txt #example-示例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-
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-
sed
sed
sed
sed
sed cv@cv:~/myfiles$ grep -n 'sed' test.txt #example-
: 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 "^sed" test.txt #example- cv@cv:~/myfiles$ grep '\<sed' test.txt #example-
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-
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-
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-
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-
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-
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-
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-
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-
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-
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-
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-
inet addr:10.249.152.52 Bcast:10.249.159.255 Mask:255.255.248.0 cv@cv:~/myfiles$ ps aux | grep 'ssh' #example-
root 0.0 0.0 ? Ss 10月24 : /usr/sbin/sshd -D
root 0.0 0.0 ? Ss : : sshd: cv [priv]
cv 0.0 0.0 ? S : : sshd: cv@notty
cv 0.0 0.0 ? Ss : : /usr/lib/openssh/sftp-server
root 0.0 0.0 ? Ss : : sshd: cv [priv]
root 0.0 0.0 ? Ss : : sshd: cv [priv]
cv 0.0 0.0 ? S : : sshd: cv@pts/
cv 0.0 0.0 ? S : : sshd: cv@notty
cv 0.0 0.0 ? Ss : : /usr/lib/openssh/sftp-server
cv 0.0 0.0 pts/ R+ : : grep --color=auto ssh cv@cv:~/myfiles$ ps aux | grep 'ssh' | grep -v 'grep' #example-
root 0.0 0.0 ? Ss 10月24 : /usr/sbin/sshd -D
root 0.0 0.0 ? Ss : : sshd: cv [priv]
cv 0.0 0.0 ? S : : sshd: cv@notty
cv 0.0 0.0 ? Ss : : /usr/lib/openssh/sftp-server
root 0.0 0.0 ? Ss : : sshd: cv [priv]
root 0.0 0.0 ? Ss : : sshd: cv [priv]
cv 0.0 0.0 ? S : : sshd: cv@pts/
cv 0.0 0.0 ? S : : sshd: cv@notty
cv 0.0 0.0 ? Ss : : /usr/lib/openssh/sftp-server
参考资料
[1] Linux grep 命令
[2] linux grep命令详解
[3] linux下的find文件查找命令与grep文件内容查找命令
Linux Bash文本操作之grep篇的更多相关文章
- Linux Bash文本操作之sed篇其二
上一篇总结了sed的基础应用(Linux Bash文本操作之sed篇其一),内容实在有够多,这里再对稍微高级一些的用法做一个整理,以方便使用时查阅. 查看文本内容 示例1表示在第一到第四行匹配到的行后 ...
- Linux Bash文本操作之sed篇其一
作为Linux系统中文本处理的强力工具之一,sed功能强大,用法多变,值得我们好好学习. sed是用于过滤和转换文本的流编辑器. 一般情况下sed把当前处理的行存储在临时缓冲区,按指定命令处理之后将缓 ...
- 【Linux】linux中文本操作利器grep,awk,sed
grep命令 grep(global search regular expression)是一种强大的文本搜索工具,它可以使用正则表达式搜索文本,并把匹配的行打印出来.平时搜索文本中内容的时候是非常方 ...
- linux 常用文本操作相关命令
平时工作经常会对文本进行相关操作,包括读写.替换.统计等等,借此整理和学习一下有关命令. 1. cat 查看文件中的内容, -n 查看时为每一行加编号; -b 和-n类似,只不过对于空白行不编号: 2 ...
- Linux - sed 文本操作
SED 是一项Linux指令,功能同awk类似,差别在于,sed简单,对列处理的功能要差一些,awk的功能复杂,对列处理的功能比较强大. sed全称是:Stream EDitor 调用sed命令有两种 ...
- linux之文本编辑器vi常用命令
由于经常在linux下面文本操作,所以这里稍微系统的总结一下自己常用的vi命令 1.打开命令: vi+filename (还有各种打开的姿势,只不过我比较顺手这个) 2.退出命令: :q 退出而 ...
- Linux命令-文件文本操作grep
文件文本操作 grep 在文件中查找符合正则表达式条件的文本行 cut 截取文件中的特定字段 paste 附加字段 tr 字符转换或压缩 sort 调整文本行的顺序,使其符合特定准则 uniq 找出重 ...
- linux下的文本操作之 文本查找——grep
摘要:你有没有这样的应用场景:调试一个程序,出现debug的提示信息,现在你需要定位是哪个文件包含了这个debug信息,也就是说,你需要在一个目录下的多个文件(可能包含子目录)中查找某个字符串的位置: ...
- 【Linux】 字符串和文本处理工具 grep & sed & awk
Linux字符串&文本处理工具 因为用linux的时候主要用到的还是字符交互界面,所以对字符串的处理变得十分重要.这篇介绍三个常用的字符串处理工具,包括grep,sed和awk ■ grep ...
随机推荐
- 2019-10-9:渗透测试,基础学习the-backdoor-factory-master(后门工厂)初接触
该文章仅供学习,利用方法来自网络文章,仅供参考 the-backdoor-factory-master(后门工制造厂)原理:可执行二进制文件中有大量的00,这些00是不包含数据的,将这些数据替换成pa ...
- 使用laravel快速构建vuepress管理器
使用laravel快速构建vuepress管理器 介绍 刚刚学了下laravel感觉很方便,最近也在用vuepress做个人博客,但是感觉每次写文章管理文章不是特别方便,就使用laravel写了这个v ...
- 堆模板(STL版)
题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...
- Djangoday1 入门及第一个apphelloworld
1 Django基础指令新建一个django project新建app创建数据库表,更新数据库表或字段使用开发服务器清空数据库创建超级管理员导出数据 导入数据Django 项目环境终端数据库命令行更多 ...
- Linux之find命令
1.find命令的作用 主要用于操作系统文件.目录的查找. 2.find命令常用参数 -name #按文件名查找 -type #按文件类型查找:b/p/c/p/l/f -size #但文件大小查找,G ...
- synchronized被这么问,谁能受得了
synchronized是面试中经常会被问到的知识点,相关的问题点也很多,问题答案涉及的知识点也很多,有经验的面试官就会顺着你的答案不断追问一下,下面的对话场景就是相关面试题的连环炮. 面试官:说一下 ...
- Spring Data JPA 条件查询的关键字
Spring Data JPA 为此提供了一些表达条件查询的关键字,大致如下: And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(Stri ...
- 小白的springboot之路(一)、环境搭建、第一个实例
小白的springboot之路(一).环境搭建.第一个实例 0- 前言 Spring boot + spring cloud + vue 的微服务架构技术栈,那简直是爽得不要不要的,怎么爽法,自行度娘 ...
- mysql那些事(4)建库建表编码的选择
mysql建数据库或者建表的时候会遇到选择编码的问题,以前我们都是习惯性的选择utf8,但是在mysql在5.5.3版本后加了utf8mb4的编码,utf8mb4可以存4个字节Unicode,mb4就 ...
- 请问1^x+2^x+3^x+\cdots +n^x的算式是什么呢?
目录 总结 请问\(1^x+2^x+3^x+\cdots +n^x\)的算式是什么呢? 一.求和式\(\sum\limits_{i=1}^n{i}\)的算式 如何证明求和简式\(\sum_{i=1}^ ...