Greetings all,
...here is yet another string of awk/sed questions from a RegExp-Challenged luser
I'm looking to have sed/awk do some clean-up on routing tables and to that end, I would like to do the following:
1.) If a line contains the word "masks" or "subnets" prepend CR/LF to the beginning of the line.
For Example, turn the following:
C 192.168.1.1/32 is directly connected, Loopback0
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
D 172.16.0.128/25
[90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
D 172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10
D 172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10
C 172.16.255.1/32 is directly connected, Loopback1
D 172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10
D 172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10
D 172.16.255.10/32
[90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
D 172.16.255.11/32
[90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
D 172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10
D 172.16.10.0/24
[90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
C 172.16.1.0/29 is directly connected, Vlan10
D 172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
D 172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001
D 172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10
[90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001
Into this:
C 192.168.1.1/32 is directly connected, Loopback0
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
D 172.16.0.128/25
[90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
D 172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10
D 172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10
C 172.16.255.1/32 is directly connected, Loopback1
D 172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10
D 172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10
D 172.16.255.10/32
[90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
D 172.16.255.11/32
[90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
D 172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10
D 172.16.10.0/24
[90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
C 172.16.1.0/29 is directly connected, Vlan10
D 172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
D 172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001
D 172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10
[90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001
2.) If a line begins with a [CDS] move this character to the end of the line.
Now the routing table snippet above would look like this:
192.168.1.1/32 is directly connected, Loopback0 C
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
172.16.0.128/25 D
[90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10 D
172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10 D
172.16.255.1/32 is directly connected, Loopback1 C
172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10 D
172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10 D
172.16.255.10/32 D
[90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
172.16.255.11/32 D
[90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10 D
172.16.10.0/24 D
[90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
172.16.1.0/29 is directly connected, Vlan10 C
172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10 D
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001 D
172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10 D
[90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001
3.) If a line begins with a "[" append it to the preceding line (i.e. insert a few backspace characters to basically over-write the CR/LF of the preceding line ?).
Now the routing table snippet above would look like this:
192.168.1.1/32 is directly connected, Loopback0 C
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
172.16.0.128/25 D [90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10 D
172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10 D
172.16.255.1/32 is directly connected, Loopback1 C
172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10 D
172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10 D
172.16.255.10/32 D [90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
172.16.255.11/32 D [90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10 D
172.16.10.0/24 D [90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
172.16.1.0/29 is directly connected, Vlan10 C
172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10 D
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001 D
172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10 D [90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001
4.) Delete all the leading space (I think this will do the trick: sed 's/^[ ]*//g')
5.) Sort the one or more lines that appear between blank lines, by IP address.
Now the routing table snippet above would look like this:
192.168.1.1/32 is directly connected, Loopback0 C
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
172.16.0.128/25 D [90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
172.16.1.0/29 is directly connected, Vlan10 C
172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10 D
172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10 D
172.16.10.0/24 D [90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10 D
172.16.255.1/32 is directly connected, Loopback1 C
172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10 D
172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10 D
172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10 D
172.16.255.10/32 D [90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
172.16.255.11/32 D [90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001 D
172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10 D [90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001
In the most general terms, I'm struggling to work with metacharacters like tab, CR/LF and backspace (1. and 3. above) and with chunks of a file that contain multiple lines (5.) in sed.
I image awk may be the better choice for this task but I'm completely unfamiliar with awk scripting.
So what do you think? Is the above even possible?
Is it a complete kludge to which you can offer a more elegant and efficient solution?
I'm novice enough in the scripting world so that just about everything I do is a bit of a kludge
...anyway...PLEASE let me know your thoughts on how I might make a large collection of routing tables easier to read/report on/summarize/further analyze or just how I might do a couple of cool new tricks with sed or awk (...or whatever other tool you think is suited to this task).
THANK YOU VERY MUCH, in advance, for any help or suggestions you may offer me!
--Steve