1

Delete new line and join it with previous line

This is partial sample output of show switch command in Cisco ACI.

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

I tried the solution given from Merge Next Line with previous 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 ~]$

It almost provided the output that I wanted, except there is extra space in it.

Desired Output 1

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

The actual problem happen when I try it with actual show switch output which has more columns.

[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 does not provided the output that I wanted.

[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 almost work, but I'm not sure what's wrong with the ----- formatting.

[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 ~]$

Another awk script attempt also producing almost similar output as previous sed script

[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 ~]$

Actual Desired Output 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    

3 Answers3

2
sed '1,2!{N;s/[[:blank:]]*\n[[:blank:]]*//;}' file

On every line outside the range 1–2:

  • append the Next line to the pattern space
  • remove leading/trailing spaces
guest
  • 2,134
1

Tell me if this works for you:

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

Using Raku (formerly known as Perl_6):

raku -e '.put for lines[0...1]; .join("").put for lines.map(*.trim).rotor: 2;'

OR

raku -e '.put for lines[0...1]; .put for lines.map(*.trim).rotor(2).map(*.join: "");'

Sample Input (1):

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

Sample Output (1):

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

Briefly, the first two (header) lines are printed with get.put xx 2;. Then lines are read in (lazily), each is trim-med to remove surrounding whitespace, and pairs of lines are grouped together with rotor: 2 (note: rotor(2) is required when not at end-of-line). To get output without a space between the grouped pairs of lines, use join(""), and print-using-terminator, a.k.a. put.

Also works for second sample file provided by OP (below).

Sample Input (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

Sample Output (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

Note: You can streamline the code above a bit, with the caveat that you'll be seeing two join calls in a row: one map-ped to join adjacent lines with no intervening space, and the second to add-back-in \n newlines between captured data (data that has been chomped by the lines routine):

raku -e 'raku -e '.put for lines[0...1]; lines.map(*.trim).rotor(2).map(*.join: "").join("\n").put;' 
jubilatious1
  • 3,195
  • 8
  • 17