Directory 1. Grep command introduction 2. Sentence format and common options 3. Reference case 3.1 Search for files starting with root in the file 3.2 Search for root appearing in the file 3.3 Search not only lines other than matching lines 3.4 Matching Partially use color to display 3.5 Only output the matching places in the file 3.6 Output the lines containing the matching string and display the number of lines 3.7 Statistics file or text summary of the number of lines containing the matching string
1. Grep command introduction The grep command in Linux system is a powerful text search tool. It can use regular expressions to search for text and copy the matching lines.
The full name of grep is GlobalRegularExpressionPrint, which represents the global regular expression version. Its usage permissions are for all users.
Chinese annotation:
grep['grep]Search target line command·global[?glo?bl]Global, filamentous regular beauty[?r?ɡj?l?] Regular, regular, regular army (n)expression beauty [?k?spr???n] Expression, expression, expression, complexion, mentality
Example sentence: It'senoughtomakeyouwetyourself,ifyou'llpardontheexpression
Linux supports three ways of grep commands: grep, egrep, grep-E
2. Sentence format and common options According to convention, we still check the help first and use grep --help
[root@mufeng test]# grep --help 用法: grep [选项]... PATTERN [FILE]... 在每个 FILE 或是标准输入中查找 PATTERN。 默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。 例如: grep -i 'hello world' menu.h main.c 正则表达式选择与解释: -E, --extended-regexp PATTERN 是一个可扩展的正则表达式(缩写为 ERE) -F, --fixed-strings PATTERN 是一组由断行符分隔的定长字符串。 -G, --basic-regexpPATTERN 是一个基本正则表达式(缩写为 BRE) -P, --perl-regexp PATTERN 是一个 Perl 正则表达式 -e, --regexp=PATTERN用 PATTERN 来进行匹配操作 -f, --file=FILE 从 FILE 中取得 PATTERN -i, --ignore-case 忽略大小写 -w, --word-regexp 强制 PATTERN 仅完全匹配字词 -x, --line-regexp 强制 PATTERN 仅完全匹配一行 -z, --null-data 一个 0 字节的数据行,但不是空行 Miscellaneous: -s, --no-messages suppress error messages -v, --invert-matchselect non-matching lines -V, --version display version information and exit --helpdisplay this help text and exit 输出控制: -m, --max-count=NUM NUM 次匹配后停止 -b, --byte-offset 输出的同时打印字节偏移 -n, --line-number 输出的同时打印行号 --line-buffered 每行输出清空 -H, --with-filename 为每一匹配项打印文件名 -h, --no-filename 输出时不显示文件名前缀 --label=LABEL 将LABEL 作为标准输入文件名前缀 -o, --only-matching show only the part of a line matching PATTERN -q, --quiet, --silent suppress all normal output --binary-files=TYPE assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match' -a, --textequivalent to --binary-files=text -Iequivalent to --binary-files=without-match -d, --directories=ACTIONhow to handle directories; ACTION is 'read', 'recurse', or 'skip' -D, --devices=ACTIONhow to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip' -r, --recursive like --directories=recurse -R, --dereference-recursive likewise, but follow all symlinks --include=FILE_PATTERN search only files that match FILE_PATTERN --exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN --exclude-from=FILE skip files matching any file pattern from FILE --exclude-dir=PATTERN directories that match PATTERN will be skipped. -L, --files-without-match print only names of FILEs containing no match -l, --files-with-matchesprint only names of FILEs containing matches -c, --count print only a count of matching lines per FILE -T, --initial-tab make tabs line up (if needed) -Z, --nullprint 0 byte after FILE name 文件控制: -B, --before-context=NUM打印以文本起始的NUM 行 -A, --after-context=NUM 打印以文本结尾的NUM 行 -C, --context=NUM 打印输出文本NUM 行 -NUMsame as --context=NUM --group-separator=SEP use SEP as a group separator --no-group-separatoruse empty string as a group separator --color[=WHEN], --colour[=WHEN] use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto' -U, --binarydo not strip CR characters at EOL (MSDOS/Windows) -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows)
In order to be more intuitive, we display the commonly used parameters in a table:
Parameter description -i ignore case -E enable POSTIX extended regular expressions -P enable perl regular expressions -o only output the matching content of the regular expression -w match whole words -v negate, that is, do not match - After n output line number has specific parameters, let’s look at the actual case:
3. Reference case
3.1 Search for files starting with root. For files starting with root, you can use ^root to view files starting with root in /etc/passwd. The operation is as follows:
[root@mufenggrow ~]# grep ^root /etc/passwd root:x:0:0:root:/root:/bin/bash
3.2 Search for the root that appears in the file and search for a certain phrase. We can directly follow the word name in front of grep:
Case 1: Search for the root user in /etc/passwd
[root@mufenggrow ~]# grep "root" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@mufenggrow ~]#
Case 2: Search root from multiple files
root@mufenggrow ~]# echo root >> a.txt [root@mufenggrow ~]# echo root >> b.txt [root@mufenggrow ~]# grep "root" /etc/passwda.txt b.txt /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin a.txt:root b.txt:root [root@mufenggrow ~]#
3.3 Search for lines other than matching lines. Use the -v parameter here, such as inversion
Case 1: Count the number of lines in the file and do not include blank lines
Expression of blank lines: ^$
[root@mufenggrow ~]# cp /etc/passwd ./ ## 源文件一共35行 [root@mufenggrow ~]# cat /etc/passwd |wc -l 35 ## 追加空行进去 [root@mufenggrow ~]# echo "" >> /etc/passwd [root@mufenggrow ~]# cat /etc/passwd |wc -l 36 ## 去掉空行测试 [root@mufenggrow ~]# grep -v ^$/etc/passwd |wc -l 35 [root@mufenggrow ~]#
Sometimes we change the configuration file and the file contains a lot of #. If we want to remove the # and view the content, we can use
[root@mufenggrow ~]# grep -v ^# passwd |wc -l 35
3.4匹配的部份使用颜色显示这儿可以使用--color=auto,我们来查看一下包含root的行linux cat grep 匹配linux cat grep 匹配,并高亮显示要查找的root。
[root@mufenggrow ~]# grep root /etc/passwd--color=auto root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@mufenggrow ~]#
这样显示,疗效不显著,我们看右图:
可
以看见,所有的root都是蓝色表示的。
3.5只输出文件中匹配到的地方例如我们要查询root,但我不想显示包含root的行RAR FOR LINUX,而是只显示要查询的内容:
此时须要使用-o参数,代码如下
[root@mufenggrow ~]# grep -o root /etc/passwd root root root root
要注意,假若一行中有10个root,这儿就显示10个,而不是只显示一个,所以3.4的案例中我们查询的时侯红帽子linux下载,包含root的有两行,但有4个root,在3.5案例中,显示了所有的root。
3.6输出包含匹配字符串的行,并显示所在的行数此处可以使用-n参数,-n会在一行的后面加上行号:例如“4:”
我们来看下代码示例:
[root@mufenggrow ~]# grep -n "root" passwd 1:root:x:0:0:root:/root:/bin/bash 11:operator:x:11:0:operator:/root:/sbin/nologin
我们要统计一个文件一共有多少行,也可以使用-n参数
root@mufenggrow ~]# grep -n "" passwd |awk -F : '{print $1}' |tail -n 1 35
3.7统计文件或则文本中包含匹配字符串的行数此时可以用-c参数:
[root@mufenggrow ~]# grep -c "root" passwd 2
包含root的有两行,假如我们要统计文本的行数:
[root@mufenggrow ~]# grep -c "$" passwd 35
相当于查找$的行数,可以见到一共有35个$符号,也就是35行。
总结grep命令在日常工作中,应用的比较广泛,一定要认真学习,记熟记牢常用参数。
到此这篇关于linux中grep命令使用实战解读的文章就介绍到这了,更多相关linuxgrep命令内容请搜索曾经的文章或继续浏览下边的相关文章希望你们之后多多支持!
The above is the detailed content of Practical case analysis and reference case analysis of grep command in Linux system. For more information, please follow other related articles on the PHP Chinese website!