grep

grep

即(global regular expression print);Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行/特定内容打印出来。

匹配特征:grep默认一些简单正则,如^,$等,

参数多;常用的也多


例子

nl /etc/passwd | grep 'qmcui'   # 最好带引号

nl /etc/passwd | grep -v 'qmcui'

nl /etc/passwd | grep -w  "root"

nl /etc/passwd | grep  ROOT
nl /etc/passwd | grep -i ROOT


nl /etc/passwd | grep -ie 'Server' -e 'root' -e 'qmcui'
nl /etc/passwd | grep 'root\|qmcui'
nl /etc/passwd | grep -ie 'Server' -e 'root\|qmcui'
## 区别下面代码
# nl /etc/passwd | grep 'root\|qmcui'       # 转义成正则
nl /etc/passwd | grep 'root' | grep 'qmcui'

echo $PATH | grep ':'
echo $PATH | grep -o ':'

echo $PATH | sed 's/:/\n/g' | grep -A 1 '/sbin'
echo $PATH | sed 's/:/\n/g' | grep '/sbin' -A 1
echo $PATH | sed 's/:/\n/g' | grep '/sbin' -B 1
echo $PATH | sed 's/:/\n/g' | grep '/sbin' -C 1

nl /etc/passwd | grep  -c  'qmcui'
nl /etc/passwd | grep -ie 'Server' -e 'root\|qmcui' -c

nl /etc/passwd | grep -n 'qmcui'

# $ cat  match.txt      # cat >match.txt
# root
# qmcui
less /etc/passwd | grep -f match.txt

unalias grep
cat /etc/passwd | grep '^root'
cat /etc/passwd | grep --color=auto 'root'
nl /etc/passwd | grep --color=auto 'bash$'

# 进阶,不要求掌握!
nl /etc/passwd | grep -E 'root|qmcui'
grep  '[^a]bb'  1.txt       # 查bb前一个字符不为a的bb
grep   'a..b'  1.txt        # 开头为a 结尾为b 长度为4
cat /etc/passwd|grep --color=auto 'o\{2\}'
cat /etc/passwd|grep --color=auto 'o\{2,\}'
seq 20 | grep '[10]' 

参数详解

grep --help: 首先学会长参数,短参数

-v –invert-match:取没有匹配的行
-w –word-regexp:只选择匹配上整个 words
-i,–ignore-case:忽略Pattern 和文件内容的大小写
-e:多个-e 可以用来描述多个不同的匹配模式 ;or

-o:-n形式输出匹配内容

-A :输出匹配行之后的num行
-B

-C NUM–context=NUM 表示前后各NUM行

-c 输出计数后的几行

-n –line-number 显示行号

-f FILE ,–file=FILE 从FILE中获得匹配的数据

--color=auto 很有用,你看的出来

#### 彩蛋:

find /public/ -name '*gz' 2>null

学习链接

参考:https://blog.csdn.net/successdd/article/details/79088926?>
https://blog.csdn.net/shenhuan1104/article/details/75852822

不好:https://blog.csdn.net/poison_biti/article/details/75581410

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 因为明天要去参加表妹的订婚宴,所以今晚上翻箱倒柜的找外出穿的衣服。这才发现我的衣服除了睡衣就是睡衣啊!简直要疯了,...
    金妈日记阅读 2,454评论 0 0
  • 手机闹钟响了一遍,我知道那不是我早上的最终时间。 我知道再次醒来的7点半,那个短发姑娘提醒我,我该对着镜子,开始不...
    肖的救赎阅读 4,605评论 4 6
  • ​一个概念 概率偏见:世界上有两种概率,一种是基于统计学的客观概率。另一种,是人类自以为是的概率,行为经济学家把它...
    2H青年阅读 2,865评论 0 4

友情链接更多精彩内容