文章目录
[隐藏]1.释义
grep
命令以行为单位过滤文本内容
2.系统帮助
用法: 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-regexp PATTERN 是一个基本正则表达式(缩写为 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-match 选择不匹配的行 -V, --version display version information and exit --help display 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 只显示匹配到的字符串本身 -q, --quiet, --silent 静默模式,不输出任何信息 --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 以递归方式读取每个目录下的所有文件; 这相当于-d 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 文件控制: -B, --before-context=NUM 打印以文本起始的NUM 行 -A, --after-context=NUM 打印以文本结尾的NUM 行 -C, --context=NUM 打印输出文本NUM 行 -NUM same as --context=NUM --group-separator=SEP use SEP as a group separator --no-group-separator use empty string as a group separator --color[=WHEN], 匹配的文本高亮显示 --colour[=WHEN] use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto' -U, --binary do not strip CR characters at EOL (MSDOS/Windows) -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows) ‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。 直接使用‘egrep’或是‘fgrep’均已不可行了。 若FILE 为 -,将读取标准输入。不带FILE,读取当前目录,除非命令行中指定了-r 选项。 如果少于两个FILE 参数,就要默认使用-h 参数。 如果有任意行被匹配,那退出状态为 0,否则为 1; 如果有错误产生,且未指定 -q 参数,那退出状态为 2。 请将错误报告给: bug-grep@gnu.org GNU Grep 主页: <http://www.gnu.org/software/grep/> GNU 软件的通用帮助: <http://www.gnu.org/gethelp/>
3.过滤出
3.1.有匹配的行
生成演示内容: cat >test.txt<<EOF root abc 123 root itbkz.com IP 192.168.1.203 #注释行 #空格注释行 EOF 过滤匹配的行: [root@itbkz.com s]#grep root test.txt root abc 123 root 递归过滤匹配的行: [root@itbkz.com s]#grep -rn root:/root /etc/ /etc/passwd-:1:root:x:0:0:root:/root:/bin/bash /etc/passwd:1:root:x:0:0:root:/root:/bin/bash
3.2.匹配行上下各一行
[root@master141 tmp]#grep -C 1 'IP 192.168.1.203' test.txt itbkz.com IP 192.168.1.203 #注释行
以上还可以将
-C 1
修改为-1
或是-A 1 -B 1
效果相同。如果是匹配行下一行使用-A 1
,如果是匹配行上一行使用-B 1
3.3.完全匹配的行
[root@itbkz.com s]#ifconfig | grep -w inet inet 192.168.1.246 netmask 255.255.255.0 broadcast 192.168.1.255 inet 127.0.0.1 netmask 255.0.0.0
3.4.小写字母开头的行
[root@itbkz.com s]#grep "^[a-z]" test.txt root abc 123 root itbkz.com
3.5.有字母的行
方法1: [root@itbkz.com s]#grep "[a-Z]" test.txt root abc 123 root itbkz.com IP 192.168.1.203 方法2: [root@itbkz.com s]#grep "[a-zA-Z]" test.txt root abc 123 root itbkz.com IP 192.168.1.203
3.6.有.的行
[root@itbkz.com s]#grep "\." test.txt itbkz.com IP 192.168.1.203
.
原本代表任意单个字符,所以需要转义符\
3.7.IP行不分大小写
[root@itbkz.com s]#grep -i ip test.txt IP 192.168.1.203
3.8.结尾带com行
[root@itbkz.com s]#grep com$ test.txt itbkz.com
3.9.空行和全空格的行
生成演示数据: [root@itbkz.com s]#cat > test.txt <<EOF abc 123 EOF 空行和全空格的行: [root@itbkz.com s]#grep -nE '^$| ' test.txt 2: 3:
第二行为空行,第三行全是空格的行
3.10.正则匹配全部IP地址
方法1: [root@itbkz.com ~]#ifconfig | grep -E "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})" -o 192.168.1.24 255.255.255.0 192.168.1.255 127.0.0.1 255.0.0.0 方法2: [root@itbkz.com ~]#ifconfig | grep -E '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})' -o 192.168.1.24 255.255.255.0 192.168.1.255 127.0.0.1 255.0.0.0 方法3: [root@itbkz.com ~]#ifconfig | grep -E '(([0-9]|[0-9]{2}|[0-9]{3})\>.){3}([0-9]|[0-9]{2}|[0-9]{3})' -o 192.168.1.246 255.255.255.0 192.168.1.255 127.0.0.1 255.0.0.0
4.过滤掉
4.1.匹配的行
[root@itbkz.com s]#grep -v root test.txt itbkz.com IP 192.168.1.203 #注释行 #空格注释行
4.2.开头注释行
[root@itbkz.com s]#grep -v ^# test.txt root abc 123 root itbkz.com IP 192.168.1.203 #空格注释行
4.3.所有注释行
[root@itbkz.com s]#grep -v '#' test.txt root abc 123 root itbkz.com IP 192.168.1.203
4.4.空行
[root@itbkz.com s]#grep -v '^$' test.txt root abc 123 root itbkz.com IP 192.168.1.203 #注释行 #空格注释行
4.5.空行和注释行
方法1: [root@itbkz.com s]#grep -Ev "^$|#" test.txt root abc 123 root itbkz.com IP 192.168.1.203 方法2: [root@itbkz.com s]#egrep -v "^$|#" test.txt root abc 123 root itbkz.com IP 192.168.1.203
4.6.开头非#的行
[root@itbkz.com s]#grep '^[^#]' test.txt root abc 123 root itbkz.com IP 192.168.1.203 #空格注释行
5.正则表达式
关于常用正则表达式参考以下文章:
正则表达式(grep|sed|awk)基础正则表达式BRE与扩展的正则表达式ERE
文章目录[隐藏] 1基础正则(BRE)2扩展正则(ERE)3特殊匹配列表 1.基础正则(BRE) .:匹配单个 […]