当前位置: 首页 > article >正文

Linux grep命令

目录

  • 一. 前期准备
  • 二. 配置项
    • 2.1 -e 配置项
    • 2.2 -h 配置项
  • 三. 正则表达式
    • 3.1 {} 或查询
    • 3.2 文件路径和查询关键词中均包含正则表达式
  • 四. zgrep


一. 前期准备

✅TEST-2023-07-11.txt

MPLE0130 Exception 123 ExecTime=
  MPLE0190 ExecTime=123
MPLE0150 TST 1234 ExecTime=454
MPLE0160 Exception 123 ExecTime=
MPLE0170 TST 1234 ExecTime=999

✅TEST-2023-09-11.txt

CCCE0130 Exception 123
MPLE0150 TST 1234
MPLE0160 Exception 123
CCCE0170 TST 1234

✅TEST-2023-10-11.txt

MPLE0130 Exception 123 ExecTime=
  MPLE0190 ExecTime=123
MPLE0150 TST 1234 ExecTime=454
MPLE0160 Exception 123 ExecTime=
MPLE0170 TST 1234 ExecTime=999

二. 配置项

2.1 -e 配置项

⏹下面两种写法相同,查询的都是MPLE0130CCCE0130

grep -a -e MPLE0130 -e CCCE0130 /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt
grep -aE 'MPLE0130|CCCE0130' /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt
[2023-11-12 18:27:49.427] grep -a -e MPLE0130 -e CCCE0130 /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt
[2023-11-12 18:27:51.967] MPLE0130 Exception 123 ExecTime=

⏹查询TEST-2023-*-11.txt文件夹中包含Exception 关键字的

grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-*-11.txt
[2023-11-12 18:27:51.970] fengyehong@ubuntu:~$ grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-*-11.txt
[2023-11-12 18:31:47.356] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:31:47.363] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
[2023-11-12 18:31:47.371] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
[2023-11-12 18:31:47.378] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
[2023-11-12 18:31:47.387] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:31:47.394] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0160 Exception 123 ExecTime=

2.2 -h 配置项

  • 先查询ExecTime的值不为空白的数据,-h 可以只匹配结果不匹配文件所在路径
  • 然后使用 grep -e "^\S" 过滤掉开头为空白的数据
  • 然后使用 awk '{print $1}' 打印第一列
grep -a -h -e "ExecTime=\S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt | grep -e "^\S" | awk '{print $1}'
[2023-11-12 18:54:27.074] fengyehong@ubuntu:~$ grep -a -h -e "ExecTime=\S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt | grep -e "^\S" | awk '{print $1}'
[2023-11-12 18:55:41.989] MPLE0130
[2023-11-12 18:55:41.990] MPLE0150
[2023-11-12 18:55:41.990] MPLE0160
[2023-11-12 18:55:41.990] MPLE0170
[2023-11-12 18:55:41.990] MPLE0130
[2023-11-12 18:55:41.990] MPLE0150
[2023-11-12 18:55:41.990] MPLE0160
[2023-11-12 18:55:41.990] MPLE0170

三. 正则表达式

3.1 {} 或查询

{}表示或,{0[6-9],10}表示从06月到10月。

grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
[2023-11-12 18:33:28.388] fengyehong@ubuntu:~$ grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
[2023-11-12 18:33:30.750] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:33:30.757] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
[2023-11-12 18:33:30.765] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
[2023-11-12 18:33:30.772] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
[2023-11-12 18:33:30.780] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:33:30.787] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0160 Exception 123 ExecTime=

⏹从两个文件名类似的文件中查询

# 从 20231104bvspl.log 或 20231104bvapp.log 中进行查询
grep -a Error ./log/.20231104bv{spl,app}.log*

3.2 文件路径和查询关键词中均包含正则表达式

⏹路径和查询关键词中均包含正则表达式

  • ^\S 表示匹配非空白字符开头的部分。
  • ExecTime=\S* 匹配以 ExecTime= 开头后跟非空白字符的部分。
# "^\S" 表示匹配非空白字符开头的部分
# "ExecTime=\S*" 匹配以 ExecTime= 开头后跟非空白字符的部分
grep -a -e "^\S" -e "ExecTime=\S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
[2023-11-12 18:36:15.874] fengyehong@ubuntu:~$ grep -a -e "^\S" -e "ExecTime=\S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
[2023-11-12 18:37:14.504] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:37:14.521] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:  MPLE0190 ExecTime=123
[2023-11-12 18:37:14.535] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0150 TST 1234 ExecTime=454
[2023-11-12 18:37:14.553] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
[2023-11-12 18:37:14.570] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0170 TST 1234 ExecTime=999
[2023-11-12 18:37:14.587] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
[2023-11-12 18:37:14.600] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0150 TST 1234
[2023-11-12 18:37:14.615] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
[2023-11-12 18:37:14.647] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0170 TST 1234
[2023-11-12 18:37:14.662] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:37:14.679] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:  MPLE0190 ExecTime=123
[2023-11-12 18:37:14.694] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0150 TST 1234 ExecTime=454
[2023-11-12 18:37:14.712] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0160 Exception 123 ExecTime=
[2023-11-12 18:37:14.727] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0170 TST 1234 ExecTime=999

四. zgrep

⏹主要用来查询压缩包中的信息

# 统计 Desktop.zip 压缩包中 fengyehong 这个单词出现的次数
zgrep -a -e fengyehong /home/fengyehong/jmw_work_space/Desktop.zip | wc -l

http://www.kler.cn/a/149299.html

相关文章:

  • 基于 Python Django 的二手房间可视化系统分析
  • LeetCode 86.分隔链表
  • pySpark乱码
  • LeetCode【0031】下一个排列
  • 算法——二分查找(leetcode704)
  • Python 小高考篇(2)字符串
  • JavaWeb后端数据库MySQL的使用
  • 平凯星辰携手教育部教育管理信息中心,助力普惠教育数字化
  • Python与设计模式--桥梁模式
  • 互联网程序设计HTML+CSS+JS
  • Debian10安装VMware Tools
  • torch::和at:: factory function的差別
  • 【问题系列】消费者与MQ连接断开问题解决方案(一)
  • Go使用logrus框架
  • Unity 轨道展示系统(DollyMotion)
  • go标准库
  • 基于协同过滤算法的音乐推荐系统的研究与实现
  • 激光雷达毫米波雷达
  • PyTorch:模型加载方法详解
  • Vue2 若依框架头像上传 全部代码
  • 建筑工程模板包工包料价格
  • Kubernetes基础(九)-标签管理
  • 【Web】攻防世界 难度3 刷题记录(1)
  • Linux 调试工具:gdb
  • 使用shell快速查看电脑曾经连接过的WiFi密码
  • 记一次简单的PHP反序列化字符串溢出