Q:

Manipulating output - awk|sed|grep

I would like to maniputate the following output like this:
Output:
S*      0.0.0.0/0 [1/0] via 1.1.1.1, MAR-LNK-VGL_DSL
                  [1/0] via 2.2.2.2, MAR-LNK-GVT_DSL
                  [1/0] via 3.3.3.3, ENT-LNK-OI_CORP
                  [1/0] via 4.4.4.4, ENT-EBT_CORP, [10/0]
S       10.0.0.0/16 [10/0] via 5.5.5.5, ENT-LNK
C       10.5.10.0/24 is directly connected, MAR-DEV-LAN
C       10.5.20.0/24 is directly connected, MAR-DEV-DMZ
C       10.5.30.0/24 is directly connected, MAR-DEV-BALANCE
C       10.6.40.0/28 is directly connected, BNC-HOM
C       10.6.40.3/32 is directly connected, BNC-HOM
B       10.7.0.0/21 [20/100] via 6.6.6.6, vpn-21fce533-0, 03:12:51
S       10.19.210.56/29 [10/0] via 7.7.7.7, MAR-EMB_MPLS
S       10.19.233.0/24 [10/0] is directly connected, VPN-HML-PP
S       10.19.252.0/24 [5/0] is directly connected, VPN-CON
B       10.19.254.0/24 [20/0] via 8.8.8.8, ENT_EBT_MPLS, 4d20h13m
S       10.20.0.4/32 [10/0] is directly connected, VPN-AX4B
S       10.33.13.70/32 [10/0] is directly connected, VPN-NEW
B       10.50.0.0/17 [20/0] via 9.9.9.9, SEC-EQNX, 00:02:11
B       10.50.128.0/17 [20/0] via 9.9.9.9, SEC-EQNX, 00:02:11
O       100.0.153.164/32 [110/200] via 99.1.152.61, HUB-MAR-03_79, 17:36:22
                         [110/200] via 99.1.101.217, HUB-MAR-04_54, 17:36:22
                         [110/200] via 99.1.101.153, HUB-MAR-04_38, 17:36:22
                         [110/200] via 99.1.151.13, HUB-MAR-03_3, 17:36:22
O       100.0.252.11/32 [110/200] via 99.1.151.109, HUB-MAR-03_15, 03:22:24
                        [110/200] via 99.1.102.17, HUB-MAR-04_68, 03:22:24

If I filter for the 100.0.153.164/32, it would print the following output:
O       100.0.153.164/32 [110/200] via 99.1.152.61, HUB-MAR-03_79, 17:36:22
                         [110/200] via 99.1.101.217, HUB-MAR-04_54, 17:36:22
                         [110/200] via 99.1.101.153, HUB-MAR-04_38, 17:36:22
                         [110/200] via 99.1.151.13, HUB-MAR-03_3, 17:36:22

If I filter for the 0.0.0.0/0, the output would be:
S*      0.0.0.0/0 [1/0] via 1.1.1.1, MAR-LNK-VGL_DSL
                  [1/0] via 2.2.2.2, MAR-LNK-GVT_DSL
                  [1/0] via 3.3.3.3, ENT-LNK-OI_CORP
                  [1/0] via 4.4.4.4, ENT-EBT_CORP, [10/0]

And if I filter for something like 10.20.0.4/32, the output would be:
S       10.20.0.4/32 [10/0] is directly connected, VPN-AX4B

But I'm not an good with it, so I can't figure it out :(

A:

If you want to match on a given string in the second field, and print out "continuation lines" after that string has matched, try this awk script.
awk -v fltr="_str_" 'BEGIN {havematch=0}
 {if ($1 ~ /^----/) {
   if (havematch == 1) {
     print $0
   }
   next
  }
  if ($2 == fltr) {
    print $0
    havematch=1
    next
  }
  {havematch=0}
 }' _file_

I have broken it up to make it easier to read.
Basically, you start by initializing the havematch variable to zero, since you do not have any matches at the beginning.  Then, you check if a line starts with "----"  If it does, and the havematch variable is set, you print out this line since it is a "continuation line"  If the line starts with "----" and the havematch is set to zero, then this is a continuation line for something else.  Don't print it.  Either way, if the line starts with "----", after the havematch check, move to the next line.
If the second field in the line matches your filter string, print out the line and set the havematch variable to one.  Move on to the next line.
If the line is not a continuation line, and the second field does not match your filter string, then that line does not match, and you do not want to print out any continuation lines.  So, set the havematch variable to zero.
Hope this helps.

