Bootstrap

Linux:通过命令查找日志文件中的某字段

工作中有用到,做个记录。


1. 查询某字段,显示行号:

  cat -n file_name|grep '查找字段'

[root@ZWZF-CWY-LZY-12 CWY]# cat -n nohup.out |grep 'JVM'
10445 2022-12-15 17:54:35.908 cwy-web [,] [main] INFO [StartupInfoLogger.java:61] - Started CwyApplication in 252.596 seconds (JVM running for 266.291)

2. 查询某字段,不显示行号:

  grep '查找字段' file_name

[root@ZWZF-CWY-LZY-12 CWY]# grep 'JVM' nohup.out
2022-12-15 17:54:35.908 cwy-web [,] [main] INFO [StartupInfoLogger.java:61] - Started CwyApplication in 252.596 seconds (JVM running for 266.291)
[root@ZWZF-CWY-LZY-12 CWY]#

 3. 统计某字段在文件中出现的行数:

  grep -o '查找字段' file_name|wc -l

[root@ZWZF-CWY-LZY-12 CWY]# grep -o JVM nohup.out |wc -l
1
;