grep命令
grep全称为Global search Regular Expression and Print out the line? grep是一个可以利用“正则表达式”进行“全局搜索”的工具; 准备一个测试文件greptest文件,并使用grep命令进行搜索[[email?protected] ~]# cat greptest grep 123 test 234 grep ABC GREP abc [[email?protected]-n93yom ~]# grep abc greptest GREP abc [[email?protected]-n93yom ~]# 1.)使用grep -i 可以不区分大小写的进行搜索 [[email?protected] ~]# grep -i abc greptest
grep ABC
GREP abc
[[email?protected]-n93yom ~]#
2.)使用grep -n 可以显示行号 [[email?protected] ~]# grep -i -n abc greptest 3:grep ABC 4:GREP abc [[email?protected]-n93yom ~]# 3.)使用grep --color 或 --color=auto 可以高亮显示,因为在Centos7中,系统默认为grep配置了别名,所以默认就是高亮的,使用alias查看别名 [[email?protected] ~]# alias alias cp=‘cp -i‘ alias egrep=‘egrep --color=auto‘ alias fgrep=‘fgrep --color=auto‘ alias grep=‘grep --color=auto‘ //默认就是高亮的 alias l.=‘ls -d .* --color=auto‘ alias ll=‘ls -l --color=auto‘ alias ls=‘ls --color=auto‘ alias mv=‘mv -i‘ alias rm=‘rm -i‘ alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘ [[email?protected]-n93yom ~]# 4.)grep -n 命令是统计匹配到的行数,abc大小写都匹配共2行 [[email?protected] ~]# grep -i -c abc greptest 2 [[email?protected]-n93yom ~]# 5.)grep -o 只打印出匹配的关键字,而不打印出,和关键字同行的其他信息 [[email?protected] ~]# grep -i -o abc greptest
ABC
abc
? 但是,如果一行中有两个相同的关键字,则会分行输出,而不会在同一行中输出 [[email?protected] ~]# cat greptest grep 123 test 234 grep ABC GREP abc abc [[email?protected]-n93yom ~]# grep -i -o -n abc greptest 3:ABC 4:abc 4:abc [[email?protected]-n93yom ~]# 6.)grep -B? B为before的意思,使用该参数,可以把搜索到的关键字前几行,也给打印出来;? ? ? ? grep?-A 为after的意思,该命令可以把搜索到的关键字后几行给打印出来 ? ? ? grep -C 则是把关键字前后的行打印出来 7.)grep - w 精确匹配到的单词,而不是模糊匹配 [[email?protected] ~]# cat greptest haha linux grep 123 kele test 234 testkkkkuui grep ABC GREP abc abc [[email?protected]-n93yom ~]# grep -i -n test greptest 5:test 234 6:testkkkkuui [[email?protected]-n93yom ~]# grep -i -n -w test greptest 5:test 234 8.) grep -v 查找不包含关键字的行 [[email?protected] ~]# cat greptest haha linux grep 123 kele test 234 testkkkkuui grep ABC GREP abc abc [[email?protected]-n93yom ~]# grep -i -n -v test greptest 1:haha 2:linux 3:grep 123 4:kele 7:grep ABC 8:GREP abc abc [[email?protected]-n93yom ~]# 9.)grep -e 可以匹配多个关键字,匹配任意一个即打印 [[email?protected] ~]# cat greptest haha linux grep 123 kele test 234 testkkkkuui grep ABC GREP abc abc [[email?protected]-n93yom ~]# grep -i -e abc -e haha greptest haha grep ABC GREP abc abc [[email?protected]-n93yom ~]# 10.)grep -q 判断是否匹配到,而不打印出来;需要配合echo $? 使用,若echo $?为0则匹配的到,若为1则未匹配到 [[email?protected] ~]# cat greptest haha linux grep 123 kele test 234 testkkkkuui grep ABC GREP abc abc [[email?protected]-n93yom ~]# grep -q haha greptest [[email?protected]-n93yom ~]# echo $? 0 [[email?protected]-n93yom ~]# grep -q haha123 greptest [[email?protected]-n93yom ~]# echo $? 1 11.)grep -P 正则表达式匹配 给定一个包含电话号码列表(一行一个电话号码)的文本文件? 示例: 假设? 987-123-4567 123 456 7890 (123) 456-7890 # Read from the file file.txt and output all valid phone numbers to stdout. grep -P ‘^(d{3}-|(d{3}) )d{3}-d{4}$‘ file.txt 12.命令列表 -a 或 --text : 不要忽略二进制的数据。 -A<显示行数> 或 --after-context=<显示行数> : 除了显示符合范本样式的那一列之外,并显示该行之后的内容。 -b 或 --byte-offset : 在显示符合样式的那一行之前,标示出该行第一个字符的编号。 -B<显示行数> 或 --before-context=<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前的内容。 -c 或 --count : 计算符合样式的列数。 -C<显示行数> 或 --context=<显示行数>或-<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前后的内容。 -d <动作> 或 --directories=<动作> : 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。 -e<范本样式> 或 --regexp=<范本样式> : 指定字符串做为查找文件内容的样式。 -E 或 --extended-regexp : 将样式为延伸的普通表示法来使用。 -f<规则文件> 或 --file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。 -F 或 --fixed-regexp : 将样式视为固定字符串的列表。 -G 或 --basic-regexp : 将样式视为普通的表示法来使用。 -h 或 --no-filename : 在显示符合样式的那一行之前,不标示该行所属的文件名称。 -H 或 --with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。 -i 或 --ignore-case : 忽略字符大小写的差别。 -l 或 --file-with-matches : 列出文件内容符合指定的样式的文件名称。 -L 或 --files-without-match : 列出文件内容不符合指定的样式的文件名称。 -n 或 --line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。 -o 或 --only-matching : 只显示匹配PATTERN 部分。 -q 或 --quiet或--silent : 不显示任何信息。 -r 或 --recursive : 此参数的效果和指定"-d recurse"参数相同。 -s 或 --no-messages : 不显示错误信息。 -v 或 --revert-match : 显示不包含匹配文本的所有行。 -V 或 --version : 显示版本信息。 -w 或 --word-regexp : 只显示全字符合的列。 -x --line-regexp : 只显示全列符合的列。 -y : 此参数的效果和指定"-i"参数相同。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |