Shell-Script

從 Cisco“show switch”輸出中刪除新行並將其與上一行連接

  • November 10, 2021

刪除新行並將其與上一行連接

show switch這是Cisco ACI中命令的部分範例輸出。

Serial Number     Name               
----------------  ------------------ 
ABCDEFGHIJ1       XYZ_ 
                 SPN_01             
ABCDEFGHIJ2       XYZ_ 
                 SPN_02             
ABCDEFGHIJ3       XYZ_ 
                 SPN_03

我嘗試了Merge Next Line 與上一行給出的解決方案

[user@linux ~]$ sed 'H;1h;$!d;g;s/\n  */ /g' sample.txt
Serial Number     Name
----------------  ------------------
ABCDEFGHIJ1       XYZ_  SPN_01
ABCDEFGHIJ2       XYZ_  SPN_02
ABCDEFGHIJ3       XYZ_  SPN_03
[user@linux ~]$

它幾乎提供了我想要的輸出,除了裡面有額外的空間。

所需輸出 1

Serial Number     Name
----------------  ------------------
ABCDEFGHIJ1       XYZ_SPN_01
ABCDEFGHIJ2       XYZ_SPN_02
ABCDEFGHIJ3       XYZ_SPN_03

當我嘗試使用show switch具有更多列的實際輸出時,就會發生實際問題。

[user@linux ~]$ cat output.txt
ID    Pod   Address          In-Band IPv4     In-Band IPv6               OOB IPv4         OOB IPv6                   Version             Flags  Serial Number     Name
----  ----  ---------------  ---------------  -------------------------  ---------------  -------------------------  ------------------  -----  ----------------  ------------------
101   1     192.168.1.10     0.0.0.0          ::                         10.1.1.10        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ1       ABC_
                                                                                                                                                                  SPN_01
102   1     192.168.1.11     0.0.0.0          ::                         10.1.1.11        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ2       ABC_
                                                                                                                                                                  SPN_02
103   1     192.168.1.12     0.0.0.0          ::                         10.1.1.12        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ3       ABC_
                                                                                                                                                                  SPN_03
[user@linux ~]$

sed 'H;1h;$!d;g;s/\n */ /g' output.txt沒有提供我想要的輸出。

[user@linux ~]$ sed 'H;1h;$!d;g;s/\n  */ /g' output.txt
ID    Pod   Address          In-Band IPv4     In-Band IPv6               OOB IPv4         OOB IPv6                   Version             Flags  Serial Number     Name                ----  ----  ---------------  ---------------  -------------------------  ---------------  -------------------------  ------------------  -----  ----------------  ------------------  101   1     192.168.1.10     0.0.0.0          ::                         10.1.1.10        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ1       ABC_  SPN_01              102   1     192.168.1.11     0.0.0.0          ::                         10.1.1.11        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ2       ABC_  SPN_02              103   1     192.168.1.12     0.0.0.0          ::                         10.1.1.12        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ3       ABC_  SPN_03
[user@linux ~]$

awk幾乎可以工作,但我不確定-----格式有什麼問題。

[user@linux ~]$ awk '{$1=$1;printf("%s ",$0)};NR%2==0{print ""}' output.txt
ID Pod Address In-Band IPv4 In-Band IPv6 OOB IPv4 OOB IPv6 Version Flags Serial Number Name ---- ---- --------------- --------------- ------------------------- --------------- ------------------------- ------------------ ----- ---------------- ------------------
101 1 192.168.1.10 0.0.0.0 :: 10.1.1.10 :: n9000-13.0(2n) asiv ABCDEFGHIJ1 ABC_ SPN_01
102 1 192.168.1.11 0.0.0.0 :: 10.1.1.11 :: n9000-13.0(2n) asiv ABCDEFGHIJ2 ABC_ SPN_02
103 1 192.168.1.12 0.0.0.0 :: 10.1.1.12 :: n9000-13.0(2n) asiv ABCDEFGHIJ3 ABC_ SPN_03
[user@linux ~]$

另一個awk腳本嘗試也產生與先前sed腳本幾乎相似的輸出

[user@linux ~]$ awk -f script.awk output.txt
ID    Pod   Address     In-Band IPv4     In-Band IPv6               OOB IPv4         OOB IPv6                   Version             Flags  Serial Number     Name                ----  ----  ---------------  ---------------  -------------------------  ---------------  -------------------------  ------------------  -----  ----------------  ------------------  101   1192.168.1.10     0.0.0.0          ::                         10.1.1.10        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ1       ABC_                                                                                                                                                               SPN_01              102   1192.168.1.11     0.0.0.0          ::                         10.1.1.11        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ2       ABC_                                                                                                                                                               SPN_02              103   1192.168.1.12     0.0.0.0          ::                         10.1.1.12        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ3       ABC_                                                                                                                                                               SPN_03
[user@linux ~]$

實際期望輸出 2

ID    Pod   Address          In-Band IPv4     In-Band IPv6               OOB IPv4         OOB IPv6                   Version             Flags  Serial Number     Name               
----  ----  ---------------  ---------------  -------------------------  ---------------  -------------------------  ------------------  -----  ----------------  ------------------ 
101   1     192.168.1.10     0.0.0.0          ::                         10.1.1.10        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ1       ABC_SPN_01             
102   1     192.168.1.11     0.0.0.0          ::                         10.1.1.11        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ2       ABC_SPN_02             
103   1     192.168.1.12     0.0.0.0          ::                         10.1.1.12        ::                         n9000-13.0(2n)      asiv   ABCDEFGHIJ3       ABC_SPN_03    
sed '1,2!{N;s/[[:blank:]]*\n[[:blank:]]*//;}' file

在範圍 1-2 之外的每一行上:

  • 將下一行附加到模式空間
  • 刪除前導/尾隨空格

告訴我這是否適合你:

sed 'H;1h;$!d;g;s/\n *  \([A-Z]*\)/\1/g' file

引用自:https://unix.stackexchange.com/questions/574546