如何在 grep 搜索的上方和下方显示 n 行?
how to show n lines above and below my grep search?
我查看了k8s的manifest文件,但我还想看看我搜索的上面n行
cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep -i authorization -A4
- --authorization-mode=Node,RBAC
- --client-ca-file=/etc/kubernetes/pki/ca.crt
- --enable-admission-plugins=NodeRestriction
- --enable-bootstrap-token-auth=true
- --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
这个怎么玩?
将 -B<n>
附加到您的命令。示例:grep -i authorization -A4 -B4
使用 -C
您可以获得匹配的上下文:
grep -C4 -i authorization /etc/kubernetes/manifests/kube-apiserver.yaml
来自 grep
手册页:
Context Line Control
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.
With the -o or --only-matching option, this has no effect and a warning is given.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.
With the -o or --only-matching option, this has no effect and a warning is given.
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or
--only-matching option, this has no effect and a warning is given.
我查看了k8s的manifest文件,但我还想看看我搜索的上面n行
cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep -i authorization -A4
- --authorization-mode=Node,RBAC
- --client-ca-file=/etc/kubernetes/pki/ca.crt
- --enable-admission-plugins=NodeRestriction
- --enable-bootstrap-token-auth=true
- --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
这个怎么玩?
将 -B<n>
附加到您的命令。示例:grep -i authorization -A4 -B4
使用 -C
您可以获得匹配的上下文:
grep -C4 -i authorization /etc/kubernetes/manifests/kube-apiserver.yaml
来自 grep
手册页:
Context Line Control
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.
With the -o or --only-matching option, this has no effect and a warning is given.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.
With the -o or --only-matching option, this has no effect and a warning is given.
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or
--only-matching option, this has no effect and a warning is given.