grep
grep 是最常用的文本搜索工具,即 globally search a regulare expression and print的缩写词,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。Unix的grep家族包括grep、egrep和fgrep。
grep最主要的用途是文本过滤,用于过滤与模式匹配的行,这个模式可以是给定的字符串或者正则表达式。可以在文件、标准输入或者标注输出处过滤。
使用文档如下
grep [OPTION]... PATTERN [FILE]...
-E, --extended-regexp 该模式是扩展后的正则表达式,使用该参数后和egrep效果相同,与egrep等价
-F, --fixed-strings 该模式是一组用换行符分隔的固定字符串,不使用正则匹配,直接匹配,与fgrep等价
-G, --basic-regexp 该模式基本的正则表达式
-P, --perl-regexp 该模式是Perl类型的正则表达式
-e, --regexp=PATTERN 使用正则模式来匹配
-f, --file=FILE 从文件FILE中寻找模式作为匹配的项
-i, --ignore-case 忽略大小写
-w, --word-regexp 强制模式只匹配整个单词,如果模式是待匹配的字符串的一部分,则匹配失败
-x, --line-regexp 强制模式只能匹配整行
-z, --null-data 匹配数据行以0字节结尾的行,而不是换行符
其他选项:
-s, --no-messages 禁止显示错误信息
-v, --invert-match 选择与模式不匹配的行,反选
-V, --version 打印grep的版本信息并退出
--help 打印帮助信息并退出
输出控制:
-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 只显示每行与模式匹配的部分
-q, --quiet, --silent 不显示正常的输出
--binary-files=TYPE 假设二进制文件类型是'binary', 'text', 或者 'without-match'
-a, --text 等效于--binary-files=text
-I 等效于--binary-files=without-match
-d, --directories=ACTION 以'read', 'recurse', 或者 'skip'的方式来处理文件夹
-D, --devices=ACTION 以'read' 或者 'skip'的方式来处理devices, FIFOs and sockets
-r, --recursive 类似--directories=recurse选项
-R, --dereference-recursive
取消引用递归
--include=FILE_PATTERN
仅仅搜索与FILE_PATTERN匹配的文件
--exclude=FILE_PATTERN
搜索与FILE_PATTERN不匹配的文件和目录
--exclude-from=FILE
跳过与FILE中的任何文件模式匹配的文件
--exclude-dir=PATTERN
与模式匹配的目录将被跳过
-L, --files-without-match 只打印不匹配文件的名称
-l, --files-with-matches 仅打印包含匹配项的文件名
-c, --count 打印与模式匹配的总行数
上下文控制选项:
-B, --before-context=NUM 打印匹配项前NUM行内容
-A, --after-context=NUM 打印匹配项后NUM行内容
-C, --context=NUM 打印与模式匹配的前后NUM行内容
-NUM 与选项--context=NUM相同
--group-separator=SEP 使用SEP作为组分隔符
--no-group-separator 使用空串作为组分隔符
--color[=WHEN],
--colour[=WHEN] 使用标记高亮突出匹配的字符串,WHEN可取的值有:'always', 'never', 或者 'auto'
-U, --binary 不剥离行尾的CR字符串
Search for PATTERN in each FILE or standard input.
即从每个文件或者标准输入中搜索满足给定的模式,可以用两种基本的方式来使用:
# 方式1直接从1到多个文件中搜索满足条件的文本
grep patten file1 file2 ...
#方式2 从标准输入中(通常用的最多的是管道)搜索满足条件的文本
cat test.txt |grep pattern
添加一个名为addcontent.txt的文件并浏览
[root@localhost testshell]# touch addcontent.txt && echo "old content" > addcontent.txt && cat addcontent.txt
old content
total 8
drwxr-xr-x. 4 root root 47 Sep 12 11:50 .
dr-xr-x---. 16 root root 4096 Sep 12 11:30 ..
-rw-r--r--. 1 root root 12 Sep 12 11:51 addcontent.txt
drwxr-xr-x. 2 root root 6 Sep 12 11:30 awk
drwxr-xr-x. 2 root root 6 Sep 12 11:30 tee
-E, --extended-regexp
扩展正则表达式
[root@localhost testshell]# grep -E "[-]+" addcontent.txt
-F, --fixed-strings
把文本按照-F后面的字符切分
grep -F ":" addcontent.txt
``
-G, --basic-regexp
系统默认支持
-P, --perl-regexp
显式指定perl正则表达式支持
-e, --regexp=PATTERN
显式指定正则表达式支持,支持多选项
grep -e "txt" -e "content" addcontent.txt
-f, --file=FILE
指定匹配文件中的正则表达式
添加一个regp.dat的文件,并追加content到文件中
echo "content" > regp.dat
grep -f regp.dat addcontent.txt
-i, --ignore-case
忽略匹配的字符的大小写
grep -i "content" addcontent.txt
-w, --word-regexp
强制要求匹配整个模式
grep -w "content" addcontent.txt
-x, --line-regexp
强制模式匹配整行
grep -x "old content" addcontent.txt
-z, --null-data
数据行以0字节结尾,而不是换行符
-s, --no-messages
不显示错误信息
-v, --invert-match
忽略匹配项,选择不匹配的内容
grep -v "content" addcontent.txt
-V, --version
显式版本信息
--help
显式帮助文档
-
输出控制参数说明及举例(未完待续。。。)
-m, --max-count=NUM
-b, --byte-offset
print the byte offset with output lines
-n, --line-number
print line number with output lines
--line-buffered
flush output on every line
-H, --with-filename
print the file name for each match
-h, --no-filename
suppress the file name prefix on output
--label=LABEL
use LABEL as the standard input file name prefix
-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, --text
equivalent to --binary-files=text
-I
equivalent to --binary-files=without-match
-d, --directories=ACTION
how to handle directories;
ACTION is ‘read’, ‘recurse’, or ‘skip’
-D, --devices=ACTION
how 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-matches
print 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, --null
print 0 byte after FILE name