首页 > 其他分享 >grep 命令详解

grep 命令详解

时间:2023-01-29 10:36:31浏览次数:41  
标签:shell grep Started 命令 详解 test 匹配 root

grep简介
grep [-acinv] [--color=auto] '匹配的字串' 文件名称
选项与参数:
-a :将二进制文件以text文件的方式匹配
-c :统计匹配到的字串次数
-i :忽略大小写
-n :输出行号
-v :反向选择,不输出匹配项
-E : 使用正则匹配
-o : 单独取出匹配项
-A : 下文匹配
-B : 下文匹配
--color=auto :将匹配项高亮颜色输出

grep -c 使用 统计root共2次

[root@shell-test ~]# grep -c root /etc/passwd
2

grep -i 使用 Started没有区分大小写

[root@shell-test log]# grep -i ****started**** messages|tail
Aug 10 04:20:01 shell-test systemd: ****Started**** Session 45 of user root.
Aug 10 04:20:01 shell-test systemd: Started Session 46 of user root.

grep -n 使用 开头为行号

grep -n Started messages|tail
105:Aug 10 04:20:01 shell-test systemd: ****Started**** Session 45 of user root.
107:Aug 10 04:20:01 shell-test systemd: ****Started**** Session 46 of user root.

grep -v 使用 反向匹配Started

grep -v Started messages|tail
Aug 10 04:20:27 shell-test systemd: Starting Session 48 of user root.
Aug 10 04:20:27 shell-test systemd-logind: New session 49 of user root

grep -E 使用 匹配两个字串

[root@shell-test log]# grep -E 'Started|Starting' messages|tail
Aug 10 04:30:01 shell-test systemd: ****Started**** Session 52 of user root.
Aug 10 04:30:01 shell-test systemd: ****Starting**** Session 52 of user root.

grep -o 使用 只取出匹配字串

[root@shell-test log]# grep -o Started messages|tail
Started
Started

grep -i root -A 3 使用 显示root下三行

grep -i root -A 3 /etc/passwd
root::0:0:root:/****root****:/bin/bash
bin::1:1:bin:/bin:/sbin/nologin
daemon::2:2:daemon:/sbin:/sbin/nologin

grep -i root -B 3 使用 显示root上三行 --color为匹配字串加颜色

grep -i root -B 3 /etc/passwd --color 
halt::7:0:halt:/sbin:/sbin/halt
mail::8:12:mail:/var/spool/mail:/sbin/nologin
operator::11:0:operator:/****root****:/sbin/nologin

grep -r '匹配字串' * #在当前目录及其子目录下搜索匹配字串行的文件
grep -l -r '匹配字串' * #在当前目录及其子目录下搜索匹配子串行的文件,但是不显示匹配的行,只显示匹配的文件

grep 正则表达式

grep -n 'r[ao]t' 匹配rot或rat的项

grep -n 'r[oa]t' /etc/passwd
10:operator:x:11:0:operator:/root:/sbin/nologin
17:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

grep -n '[^r]oo' 匹配不是r开头后面跟oo的项

[root@shell-test log]# grep -n '[^r]oo' /etc/passwd
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
22:loop:x:1001:1001::/home/loop:/bin/bash

grep -n '^[r]oo' 匹配r开头后面跟oo的项

[root@shell-test log]# grep -n '^[r]oo' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
egrep  '^[0-9]' 匹配0-9开头的项这个是个例子实际需要灵活应用
egrep  '^\.' \是转义符把.转换为普通的点匹配以.开头的隐藏文件
egrep  '^$' 匹配出空白行
egrep  .. 是匹配两个字符
egrep  * 是匹配任意字符
egrep  a+ 出现一次或多次
egrep a.? a后面跟0个或1个项
egrep -n 'o\{2\}' 匹配出现两次o的项
egrep -n 'o\{3,5\}' 匹配出现3-5次o的项

标签:shell,grep,Started,命令,详解,test,匹配,root
From: https://www.cnblogs.com/YINBin/p/17071916.html

相关文章

  • shell/Linux 任务学习笔记整理2:head/tail命令
    注!!:笔记来源:(原文链接:)https://blog.csdn.net/zznnniuu/article/details/123155074      版权声明:本文为CSDN博主「zznnniuu」的原创文章原文链接:https://blog......
  • Git命令
    环境配置//配置用户信息gitconfig--globaluser.name"name"gitconfig--globaluser.email"email"//查看所有用户配置gitconfig--list//查看user.name配......
  • CentOS网络服务操作命令
    CentOS重启网络服务,输入下面的命令:systemctlretartnetwork.service或systemctlrestartnetwork。CentOS启动网络服务,输入下面的命令:systemctlstartnetwork.servi......
  • Linux-crontab定期执行程序的命令
    https://www.runoob.com/linux/linux-comm-crontab.html Linux crontab 是用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。crond 命......
  • linux命令2
    ⽹络不通排查流程1.确认⽹关地址是否通畅2.确认⽹卡配置是否正确3.确认⽹络管理服务关闭 systemctlstopNetworkManager systemctldisableNetworkManageret......
  • bash:"IF command"命令作为条件
    bash:"IFcommand"命令作为条件    一、说明 1、IF的条件部分,可以使用linux“命令”。1ifcommand2then3commandif$?=04else5command......
  • linux基础命令
    linux基础命令前期必备知识1.命令提示符[root@jasonNB~]#root表示⽤户名信息@为分隔符jasonNB为主机名称~表示所处⽬录位置......
  • Anaconda和pip常用命令汇总:简单,通俗易懂
    前言在学习anaconda的常用命令之前要先安装好AnacondaforWindows,AnacondaforUbuntu,这里我总结了Windows环境下和Ubuntu环境下的常用命令,包含了Anaconda本身......
  • Zookeeper原理详解
    一、Zookeeper原理详解 1、Zookeeper是什么Zookeeper分布式服务框架是ApacheHadoop的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命......
  • flutter 常用命令
    相关项目操作查看Flutter版本查看当前版本flutter--version查看所有版本flutterversion打印所有命令行用法信息 flutter-h或者flutter......