Awk
如何將第 4 個欄位合併為 1 行而不是 2
這是
show version
Cisco APIC 中的輸出。正如你所看到的,它有奇怪的輸出,控制器的主機名
AB_CDE_FGH_IJ_ACI_APC-0001
被分成兩行。其他主機名也是如此。
show version
Cisco APIC 中的實際輸出AB_CDE_FGH_IJ_ACI_APC_0001# show version Role Pod Node Name Version ---------- ---------- ---------- ------------------------ -------------------- controller 1 1 AB_CDE_FGH_IJ_ACI_ 3.2(4e) APC-0001 controller 2 2 AB_CDE_FGH_IJ_ACI_ 3.2(4e) APC-0002 controller 3 3 AB_CDE_FGH_IJ_ACI_ 3.2(4e) APC-0003 spine 1 111 AB_CDE_FGH_IJ_ACI_SPN_01 n9000-13.2(4e) 11 spine 1 112 AB_CDE_FGH_IJ_ACI_SPN_01 n9000-13.2(4e) 12 spine 1 113 AB_CDE_FGH_IJ_ACI_SPN_01 n9000-13.2(4e) 13 leaf 1 1101 AB_CDE_FGH_IJ_ACI_LEF_11 n9000-13.2(4e) 01 leaf 1 1102 AB_CDE_FGH_IJ_ACI_LEF_11 n9000-13.2(4e) 02 leaf 1 1103 AB_CDE_FGH_IJ_ACI_LEF_11 n9000-13.2(4e) 03
我不確定我是否可以在 Cisco ACI 本身中解決此問題,但我認為這可能可以通過將 Cisco APIC輸出保存到文件然後在 Linux 機器上執行腳本來解決
sed
。awk``show version``show_version.txt
sed
不幸的是,我並不擅長awk
編寫腳本。有沒有辦法產生這樣的輸出?
[user@Linux ~]$ sed_or_awk_script_here show_version.txt AB_CDE_FGH_IJ_ACI_APC_0001# show version Role Pod Node Name Version ---------- ---------- ---------- ------------------------ -------------------- controller 1 1 AB_CDE_FGH_IJ_ACI_APC_0001 3.2(4e) controller 2 2 AB_CDE_FGH_IJ_ACI_APC_0002 3.2(4e) controller 3 3 AB_CDE_FGH_IJ_ACI_APC_0003 3.2(4e) spine 1 111 AB_CDE_FGH_IJ_ACI_SPN_0111 n9000-13.2(4e) spine 1 112 AB_CDE_FGH_IJ_ACI_SPN_0112 n9000-13.2(4e) spine 1 113 AB_CDE_FGH_IJ_ACI_SPN_0113 n9000-13.2(4e) leaf 1 1101 AB_CDE_FGH_IJ_ACI_LEF_1101 n9000-13.2(4e) leaf 1 1102 AB_CDE_FGH_IJ_ACI_LEF_1102 n9000-13.2(4e) leaf 1 1103 AB_CDE_FGH_IJ_ACI_LEF_1103 n9000-13.2(4e) [user@Linux ~]$
使用
awk
並假設第一行show version
是空行(使其成為三個標題行):awk ' NR<4{ print; next } # print 3 header lines NF>1{ # if number of fields > 1 sub($4, $4 "%s" ) # append `%s` to 4th field fmtstr=$0 # save modified line in `fmtstr` next # continue with next line } { printf fmtstr "\n", $1 } # print `fmtstr` with first field as argument ' show_version.txt
輸出:
Role Pod Node Name Version ---------- ---------- ---------- ------------------------ -------------------- controller 1 1 AB_CDE_FGH_IJ_ACI_APC-0001 3.2(4e) controller 2 2 AB_CDE_FGH_IJ_ACI_APC-0002 3.2(4e) controller 3 3 AB_CDE_FGH_IJ_ACI_APC-0003 3.2(4e) spine 1 111 AB_CDE_FGH_IJ_ACI_SPN_0111 n9000-13.2(4e) spine 1 112 AB_CDE_FGH_IJ_ACI_SPN_0112 n9000-13.2(4e) spine 1 113 AB_CDE_FGH_IJ_ACI_SPN_0113 n9000-13.2(4e) leaf 1 1101 AB_CDE_FGH_IJ_ACI_LEF_1101 n9000-13.2(4e) leaf 1 1102 AB_CDE_FGH_IJ_ACI_LEF_1102 n9000-13.2(4e) leaf 1 1103 AB_CDE_FGH_IJ_ACI_LEF_1103 n9000-13.2(4e)