01shell脚本之文本处理工具grep(行过滤工具)
grep是行过滤工具,用于根据关键字进行行过滤。
语法:grep [选项] '关键字' 文件名
#选项--color=auto 设置关键字颜色 [root@bogon tmp]# grep --color=auto 'root' passwd

#设置别名(临时) [root@bogon tmp]# alias grep='grep --color=auto'

#设置别名(永久生效)---->修改配置文件(/etc/bashrc ) [root@bogon tmp]# vim /etc/bashrc

[root@bogon tmp]# source /etc/bashrc #重新加载/etc/bashrc [root@bogon tmp]# su jsy [jsy@bogon tmp]$ grep 'root' passwd

# 参数 -n显示行号 [root@bogon tmp]# grep -n 'root' passwd

# -i 不区分大小写匹配 [root@bogon tmp]# grep -ni 'root' passwd

#^ 匹配开头 [root@bogon tmp]# grep '^root' passwd

#-v反向匹配 [root@bogon tmp]# grep -nv '^root' passwd

#匹配所在行的前几行 [root@bogon tmp]# grep -n -B3 '^ftp' passwd #查看开头为ftp所在行及其前面3行

#匹配所在行的前后几行 [root@bogon tmp]# grep -n -C3 '^ftp' passwd #查看开头为ftp所在行及其前后3行


浙公网安备 33010602011771号