04_04_linux_grep
2016-11-13 21:23
232 查看
文本查找的需要(grep、egrep、fgrep)
模式pattern,文本字符和正则表达式的元字符组合而成匹配条件
grep:根据模式,搜索文本,并将符合模式的文本显示出来
grep [OPTIONS] PATTERN [FILE...]
--color:颜色标出
-v:反向查找
-o:只显示被匹配的串
正则表达式:regular expression、REGEXP
元字符:
.:任意单个字符
匹配次数:
*:匹配其前面的字符任意次
例子:a*b 匹配b任意次
\?:匹配前面字符1次或0次
\{m,n\}:匹配其前面的字符至少m次,至多n次
位置锚定
^:锚定行首,出现在前面
^$ :空白行
[^]:匹配指定范围外的任意单个字符
字符集合:[:digit:] [:lower:][:upper:][:alnum:][:punct:][:space:]
\<或\b:其后面的任意字符必须作为单词首部出现
\>或\b:其前面的任意字符必须作为单词尾部出现
分组:
\(\)
\(ab\)*:后向应用ab可以出现任意次
\1:引用第一个左括号出现的词组一次
模式pattern,文本字符和正则表达式的元字符组合而成匹配条件
grep:根据模式,搜索文本,并将符合模式的文本显示出来
grep [OPTIONS] PATTERN [FILE...]
[root@localhost /]# grep 'root' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin-i:忽略大小写
--color:颜色标出
-v:反向查找
-o:只显示被匹配的串
[root@localhost /]# grep -o 'root' /etc/passwd root root root root
正则表达式:regular expression、REGEXP
元字符:
.:任意单个字符
[root@localhost /]# grep -o 'r..t' /etc/passwd root root root root r/ft
匹配次数:
*:匹配其前面的字符任意次
例子:a*b 匹配b任意次
[root@localhost /]# grep 'a*b' /tmp/greptest b ab aab acb anmnb.*:a.*b a和b间任意字符
[root@localhost /]# grep 'a.*b' /tmp/greptest ab aab acb anmnb
\?:匹配前面字符1次或0次
[root@localhost /]# grep --color 'a\?b' /tmp/greptest b ab aab acb anmnb [root@localhost /]# grep --color 'a\?' /tmp/greptest a b ab aab acb anmnb
\{m,n\}:匹配其前面的字符至少m次,至多n次
位置锚定
^:锚定行首,出现在前面
[root@localhost /]# grep '^r..t' /etc/passwd root:x:0:0:root:/root:/bin/bash$:锚定行尾,出现在后面
[root@localhost /]# grep --color 'b$' /tmp/greptest b ab aab acb anmnb [root@localhost /]# grep --color 'a$' /tmp/greptest a
^$ :空白行
[root@bogon ~]# grep '^$' /etc/passwd | wc -l 8[]:匹配指定范围内的任意单个字符
[^]:匹配指定范围外的任意单个字符
字符集合:[:digit:] [:lower:][:upper:][:alnum:][:punct:][:space:]
\<或\b:其后面的任意字符必须作为单词首部出现
[root@bogon ~]# grep "\<root" test4.txt this is root. the user is root. rooter is dog's name
\>或\b:其前面的任意字符必须作为单词尾部出现
[root@bogon ~]# grep "root\>" test4.txt this is root. the user is root. chroot is a command.
[root@bogon ~]# grep "\<root\>" test4.txt this is root. the user is root.
分组:
\(\)
\(ab\)*:后向应用ab可以出现任意次
\1:引用第一个左括号出现的词组一次
[root@bogon /]# grep 'l..e.*l..e' test/txt he love his lover. she like his lover. he like his liker. she love her liker. [root@bogon /]# grep '\(l..e\).*\1' test/txt he love his lover. he like his liker.
相关文章推荐
- [鸟哥linux视频教程整理]04_04_grep及正则表达式
- Linux下的Grep命令使用方法详细介绍
- linux grep命令的使用
- 写得蛮好的linux学习笔记 [转] 三vi,shell,grep使用
- 短期快速学习LINUX,涉及vi用法,管道,标准输入、输出和错误,grep 用法
- linux命令--grep
- linux正则表达式 grep egrep用法
- Linux之grep的使用
- 关于Linux Grep命令使用的详细介绍
- linux grep命令,强大的文本搜索
- linux grep命令的使用
- linux笔记04(用户和组管理)
- Ubuntu/Linux入门介绍(04)-Ubuntu系统目录结构
- Linux Commads grep
- linux grep
- linux grep命令的使用
- The usage of grep under LINUX systems.
- Linux Grep命令
- linux grep搜索命令的使用方法
- linux下文本处理命令sed,awk,grep