一 grep 是干什么的
grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。
二 grep 语法
grep [ -acinv ] [ -A] [ -B ] [ --color=auto ] ‘ 查找字符串 ‘ filename
参数
-a:将binary文件已text文件的方式查找数据
[[email protected] etc]# ls | grep -a ‘UP‘ UPower
-c:计算找到 “查找字符串” 的次数
[[email protected] etc]# ls | grep -c ‘UP‘ 1
-i:忽略大小写
[[email protected] etc]# ls | grep -i ‘UP‘ backup centos-release-upstream cups cupshelpers gnupg group group- setuptool.d updatedb.conf UPower wpa_supplicant
-n:出处行号
[[email protected] etc]# ls | grep -n ‘UP‘ 289:UPower
-v:反向选择,即显示没有“查找字符串”内容的那一行
[[email protected] etc]# ls | grep -v ‘UP‘ abrt adjtime aliases aliases.db alsa alternatives ... ...
-A:后面可加数字,为after的意思,除了列出该行外,后续的 n 行 也列出来。
-B:后面可加数字,为before的意思,除了列出该行外,前面的 n 行也列出来。
[[email protected] etc]# ls -al total 2084 ... ... drwxr-xr-x 2 root root 4096 Jun 20 02:56 udisks2 drwxr-xr-x 2 root root 4096 Jun 20 02:55 unbound -rw-r--r-- 1 root root 163 Jun 13 16:23 .updated -rw-r--r-- 1 root root 557 Apr 11 04:32 updatedb.conf drwxr-xr-x 2 root root 4096 Jun 20 02:55 UPower -rw-r--r-- 1 root root 1523 Apr 11 07:48 usb_modeswitch.conf -rw-r--r--. 1 root root 37 Oct 15 2017 vconsole.conf -rw-r--r-- 1 root root 1982 Apr 11 07:54 vimrc ... ...
的确如此。但是 隐藏文件并不包含在数字中。
[[email protected] etc]# ls | grep -A 2 -B 3 ‘UP‘ udisks2 unbound updatedb.conf UPower usb_modeswitch.conf vconsole.conf
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。