grep常用过滤

前后行 A B C

grep -A 显示匹配指定内容及之后的n行

grep -B 显示匹配指定内容及之前的n行

grep -C 显示匹配指定内容及其前后各n行

1
2
3
4
5
6
7
8
9
10
11
12
cs@debian:~/oss/hexo$ cat /opt/nginx/logs/k8s-access.log | grep -C 5 "2022:15:43:27"
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:22 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:23 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:24 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:25 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:26 +0800] 502 0
127.0.0.1 - 192.168.56.103:6443, 192.168.56.101:6443, 192.168.56.102:6443 - [31/Jul/2022:15:43:27 +0800] 502 0, 0, 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:28 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:29 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:30 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:30 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:31 +0800] 502 0

与操作

多次匹配

1
2
3
4
5
6
7
8
9
10
11
12
cs@debian:~/oss/hexo$ cat /opt/nginx/logs/k8s-access.log | grep "2022:15:43:2" | grep 502
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:20 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:21 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:21 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:22 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:23 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:24 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:25 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:26 +0800] 502 0
127.0.0.1 - 192.168.56.103:6443, 192.168.56.101:6443, 192.168.56.102:6443 - [31/Jul/2022:15:43:27 +0800] 502 0, 0, 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:28 +0800] 502 0
127.0.0.1 - k8s-apiserver - [31/Jul/2022:15:43:29 +0800] 502 0

或操作 |

1
2
cs@debian:~/oss/hexo$ cat /opt/nginx/logs/k8s-access.log | grep "502\|15:43:3\|kube-apiserver"

grep -E “502|15:43:3|kube-apiserver”

egrep “502|15:43:3|kube-apiserver”

awk “502|15:43:3|kube-apiserver” file

特殊字符 fgrep

搜索文件包含正则表达式元字符串时,例如$^/等,fgrep很有用

1
2
3
4
cs@debian:~/oss/hexo$ egrep "^Hello"  12
cs@debian:~/oss/hexo$ grep "^Hello" 12
cs@debian:~/oss/hexo$ fgrep "^Hello" 12
^Hello\

不解析正则表达式、想搜什么就跟什么

压缩文件 zgrep

1
zgrep  pattern1  ./*   |  grep  pattern2

zegrep

zcat file1.gz file2.gz

xargs

参数传递

xargs -Ixargs -i是一样的,只是-i默认使用大括号作为替换符号,-I可以指定其他符号、字母、数字作为替换符号,但最好是用引号包起来

1
2
3
4
5
6
7
8
9
 ❯ brctl show | grep br- | awk '{print $1}' | xargs -i echo {}
br-0ab30bce63a6
br-3752fdd6ce99


❯ brctl show | grep br- | awk '{print $1}' | xargs -i echo "if {} down"
if br-0ab30bce63a6 down
if br-3752fdd6ce99 down

在命令中不能使用{},例如touch {1...10}.log

1
❯  brctl show | grep br- | awk '{print $1}' | xargs -I '#' echo "sudo ifconfig # down"

brctl show | grep br- | awk ‘{print $1}’ | xargs -I / echo “sudo ifconfig / down”

分割

使用 -d 命令指定分隔符

1
2
3
❯ brctl show | grep br- | awk '{print $1}' | xargs -d -
br 0ab30bce63a6
br 3752fdd6ce99

-E 截取 (-E 选项在使用了 -0 或 -d 选项时不生效)

1
2
❯ brctl show | grep br- | awk '{print $1}' | xargs  -E br-a489d33c0217
br-0ab30bce63a6 br-3752fdd6ce99 br-584f6987ca12 br-5fd932453df0 br-61f46a216249 br-6777048fb40b br-8f1afcbb751d

指定的命令行参数之前的参数(不包括-E指定的这个参数br-a489d33c0217)

该参数前后空格才能匹配

划批

-n 每次传递几个参数给其后面的命令执行

1
2
3
4
5
6
❯ brctl show | grep br- | awk '{print $1}' | xargs -E br-a489d33c0217 -n 3
br-0ab30bce63a6 br-3752fdd6ce99 br-584f6987ca12
br-5fd932453df0 br-61f46a216249 br-6777048fb40b
br-8f1afcbb751d


-oP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@k8s01 opt]# cat  /etc/kubernetes/manifests/etcd.yaml  | grep initial-cluster= |  grep -oP '(?<=://)[^/,]+'
192.168.122.11:2380
192.168.122.12:2380
192.168.122.13:2380
[root@k8s01 opt]# cat /etc/kubernetes/manifests/etcd.yaml | grep initial-cluster= | grep -oP '(?<=k8s0)[^, ]+'
1=https://192.168.122.11:2380
2=https://192.168.122.12:2380
3=https://192.168.122.13:2380
[root@k8s01 opt]# cat /etc/kubernetes/manifests/etcd.yaml | grep initial-cluster= | grep -oP '(?<=k8s0)[^=, ]+'
1
2
3
[root@k8s01 opt]# cat /etc/kubernetes/manifests/etcd.yaml | grep initial-cluster= | grep -oP '(?<=k8s0)[^, ]+'
1=https://192.168.122.11:2380
2=https://192.168.122.12:2380
3=https://192.168.122.13:2380
[root@k8s01 opt]# cat /etc/kubernetes/manifests/etcd.yaml | grep initial-cluster= | grep -oP '(?<=k8s0[1-3])[^, ]+'
=https://192.168.122.11:2380
=https://192.168.122.12:2380
=https://192.168.122.13:2380
[root@k8s01 opt]# cat /etc/kubernetes/manifests/etcd.yaml | grep initial-cluster= | grep -oP '(?<=k8s0[1-3]=)[^, ]+'
https://192.168.122.11:2380
https://192.168.122.12:2380
https://192.168.122.13:2380
[root@k8s01 opt]# cat /etc/kubernetes/manifests/etcd.yaml | grep initial-cluster= | grep -oP '(?<=k8s0[1-3]=)[^, ]+' | tr '\n' ',' | sed 's/:2380/:2379/g;s/,$//'
https://192.168.122.11:2379,https://192.168.122.12:2380,https://192.168.122.13:2380
[root@k8s01 opt]#
1
2
3
4
5
[root@k8s01 opt]# cat  /etc/kubernetes/manifests/etcd.yaml  | grep initial-cluster= | grep -oE 'https://([0-9]{1,3}\.){3}[0-9]{1,3}:2380'
https://192.168.122.11:2380
https://192.168.122.12:2380
https://192.168.122.13:2380

-rl

递归搜索

1
grep -rl  --max-count=5  "error" /home/cs --include \*.{txt,js} --binary-files=text

-l –file-with-matches : 列出文件内容符合指定的样式的文件名称

-L -files-without-match : 列出文件内容符合指定的样式的文件名称

–max-count 只显示前 5 行

–include 文本的所有 .txt 和 .js 文件

–binary-files 忽略二进制文件

点击打赏
文章目录
  1. 1. 前后行 A B C
  2. 2. 与操作
  3. 3. 或操作 |
  4. 4. 特殊字符 fgrep
  5. 5. 压缩文件 zgrep
  6. 6. xargs
    1. 6.1. 参数传递
    2. 6.2. 分割
    3. 6.3. 划批
  7. 7. -oP
  8. 8. -rl
载入天数...载入时分秒... ,