NR代表行数
举个例子:
student@k8s-master:~$ kubectl get node NAME STATUS ROLES AGE VERSION k8s-master.lab.example.com Ready control-plane,master 18d v1.20.6 k8s-node1.lab.example.com Ready,SchedulingDisabled <none> 18d v1.20.4 k8s-node2.lab.example.com Ready <none> 18d v1.20.0 k8s-node3.lab.example.com Ready <none> 18h v1.20.0
打印出第三行第二个字段
student@k8s-master:~$ kubectl get node |awk 'NR==3{print$2}' Ready,SchedulingDisabled
默认情况下分隔符是空
下面指定分隔符为|
,需要使用参数-F
指定
举个例子
student@k8s-master:~$ cat 1.txt NAME | STATUS | ROLES AGE VERSION k8s-master.lab.example.com | Ready | control-plane,master 18d v1.20.6 k8s-node1.lab.example.com | Ready,SchedulingDisabled | <none> 18d v1.20.4 k8s-node2.lab.example.com | Ready | <none> 18d v1.20.0 k8s-node3.lab.example.com | Ready | <none> 19h v1.20.0
打印第二行第三个字段
student@k8s-master:~$ awk -F "|" 'NR==2{print$3}' 1.txt control-plane,master 18d v1.20.6
这个时候,被符号|
分割的第三个字段就是control-plane,master 18d v1.20.6