Home > Linux > Sed Tips

Sed Tips


Note: This sed tips are more focused on how sed can be used along with Cisco NXOS to format the output. Some syntax may be slightly different from the linux one, but concepts remain the same

    1. Simple Find and replace : How to use sed to replace multiple spaces in an output with a single space
      show ip arp | sed ‘s: \+: :g’
      Note: 
      The sed command will look for zero or more(+) spaces and replace it with a single space

      N6K-SPINE3# show ip arp | head lines 11
      
      Flags: * - Adjacencies learnt on non-active FHRP router
             + - Adjacencies synced via CFSoE
             # - Adjacencies Throttled for Glean
             D - Static Adjacencies attached to down interface
      
      IP ARP Table for context default
      Total number of entries: 7631
      Address         Age       MAC Address     Interface
      37.40.51.101    00:18:33  5801.0500.0028  Vlan3740
      37.40.51.102    00:18:33  5801.0500.0050  Vlan3740
      % cli: command canceled
      N6K-SPINE3# show ip arp | head lines 11 | sed 's: \+: :g'
      
      Flags: * - Adjacencies learnt on non-active FHRP router
       + - Adjacencies synced via CFSoE
       # - Adjacencies Throttled for Glean
       D - Static Adjacencies attached to down interface
      
      IP ARP Table for context default
      Total number of entries: 7631
      Address Age MAC Address Interface
      37.40.51.101 00:18:41 5801.0500.0028 Vlan3740
      37.40.51.102 00:18:40 5801.0500.0050 Vlan3740
      % cli: command canceled
      N6K-SPINE3#
      
    2. Advanced Find and Replace: How to remove duplicate spaces in an output as well as replace space between two words(alpha numeric) with a hyphen(-).
      Note: This is really useful when we are using cut command to select columns with a delimiter as space. Some outputs will have column members having values with space and we need to replace those spaces with hyphen else the cut logic will get screwed up

      N6K-SPINE3# show platform afm info copp-tbls 
      COPP table                                   
      ----------                                   
      * mgmt/ipv6-mgmt - http,snmp,ntp,telnet,ssh,ftp
      ** excp/ipv6-excp - mtu failure, martian address, punt, l3 header errors, icmp errors
      
      Policer-Num  Name                  CIR       Burst     Passed-Bytes  Dropped-Bytes
      ----------------------------------------------------------------------------------
      0            default               64000     6250      41239419        0             
      1            stp                   2500000   4687      12456664        0             
      2            lacp                  128000    4687      171548112       0             
      3            fc control            3750000   65535     0               0             
      4            fc errors             125000    65535     0               0             
      5            fc data               125000    65535     0               0             
      6            lldp                  256000    4687      428894355       0             
      7            sat control           62500000  65535     0               0             
      8            sat data              250000    16000     0               0             
      9            udld                  256000    4687      170310698       0             
      10           igmp/ipv6-mld         128000    63        1998396656      200172        
      11           fip                   128000    4687      0               0             
      13           dhcp/ipv6-dhcp        128000    4687      9555449676      1213208586    
      14           L3 data hi            2500000   4096      0               0             
      15           L3 data low           1250000   512       0               0             
      16           L3 front panel rdt    250000    2048      0               0
      17           isis                  128000    4687      6499005632      0
      18           cdp                   128000    4687      186341581       0
      19           dot1x                 125000    65535     0               0
      20           pim/ipv6-pim          128000    4687      4997316070      0
      21           mgmt/ipv6-mgmt*       1500000   4687      0               0
      23           arp/ipv6-nd           128000    3515      1373801198      0
      24           rip                   1200000   4687      0               0
      25           ospf/ipv6-ospf        1200000   4687      115913712       0
      26           eigrp/ipv6-eigrp      1200000   4687      0               0
      27           hsrp vrrp/ipv6-hsrp   128000    250       119511901630    0
      29           bgp/ipv6-bpg          1200000   4687      0               0
      30           mcast first hop       1200000   4687      0               0
      31           ping                  8000      3515      0               0
      33           mcast rpf fail        64000     3125      0               0
      35           ucast fib miss        8000      15        122942          2313730
      36           glean                 128000    4687      27752854        51827500
      37           msdp                  1200000   4687      0               0
      38           excption ttl          8000      3125      0               0
      39           exception same intf   8000      3125      0               0
      40           exception ip frag     8000      3125      0               0
      41           excp/ipv6-excp**      8000      4687      1734386         0
      42           bfd                   1200000   4687      5277193652      0
      43           mcast last hop        64000     3125      0               0
      44           wccp                  132500    4687      0               0
      45           onep dpss             78125     3125      0               0
      46           ecp                   800000    3125      0               0
      47           vxlan                 128000    4687      0               0
      48           nvgre                 125000    65535     0               0
      49           L3 isis               128000    4687      0               0
      50           CFSoE Rel             62500000  65535     0               0
      ----------------------------------------------------------------------------------
      
      
      N6K-SPINE3#
      

      Note: In the above output below fields in the 2nd column has spaces in between the entry like “sat control”, “sat data”, “L3 data hi”, “L3 data low”, “L3 front panel rdt”, “hsrp vrrp/ipv6-hsrp”, “mcast rpf fail”, “ucast fib miss”, “excption ttl”, “exception same intf”, “exception ip frag”, “mcast last hop”, “L3 isis”, “CFSoE Rel”

      Problem: A use case where we need to print only first three columns. For the aforementioned fields with SPACE we are unable to see the CIR 
      N6K-SPINE3# show platform afm info copp-tbls | sed 's: \+: :g' | cut -d " " -f 1-3
      COPP table                                                                        
      ----------                                                                        
      * mgmt/ipv6-mgmt -                                                                
      ** excp/ipv6-excp -                                                               
      
      Policer-Num Name CIR
      ----------------------------------------------------------------------------------
      0 default 64000                                                                   
      1 stp 2500000                                                                     
      2 lacp 128000                                                                     
      3 fc control                                                                      
      4 fc errors                                                                       
      5 fc data                                                                         
      6 lldp 256000                                                                     
      7 sat control                                                                     
      8 sat data                                                                        
      9 udld 256000                                                                     
      10 igmp/ipv6-mld 128000                                                           
      11 fip 128000                                                                     
      13 dhcp/ipv6-dhcp 128000                                                          
      14 L3 data                                                                        
      15 L3 data                                                                        
      16 L3 front
      17 isis 128000
      18 cdp 128000
      19 dot1x 125000
      20 pim/ipv6-pim 128000
      21 mgmt/ipv6-mgmt* 1500000
      23 arp/ipv6-nd 128000
      24 rip 1200000
      25 ospf/ipv6-ospf 1200000
      26 eigrp/ipv6-eigrp 1200000
      27 hsrp vrrp/ipv6-hsrp
      29 bgp/ipv6-bpg 1200000
      30 mcast first
      31 ping 8000
      33 mcast rpf
      35 ucast fib
      36 glean 128000
      37 msdp 1200000
      38 excption ttl
      39 exception same
      40 exception ip
      41 excp/ipv6-excp** 8000
      42 bfd 1200000
      43 mcast last
      44 wccp 132500
      45 onep dpss
      46 ecp 800000
      47 vxlan 128000
      48 nvgre 125000
      49 L3 isis
      50 CFSoE Rel
      ----------------------------------------------------------------------------------
      
      
      N6K-SPINE3#
      

      Sed command to the Rescue

      N6K-SPINE3# show platform afm info copp-tbls | sed 's: \+: :g' | sed 's:\([A-Z,a-z][0-9]*\) \([A-Z,a-z][0-9]*\):\1\-\2:g' | cut -d " " -f 1-3
      COPP-table                                                                                                                                   
      ----------                                                                                                                                   
      * mgmt/ipv6-mgmt -                                                                                                                           
      ** excp/ipv6-excp -                                                                                                                          
      
      Policer-Num-Name-CIR-Burst-Passed-Bytes-Dropped-Bytes
      ----------------------------------------------------------------------------------
      0 default 64000                                                                   
      1 stp 2500000                                                                     
      2 lacp 128000                                                                     
      3 fc-control 3750000                                                              
      4 fc-errors 125000                                                                
      5 fc-data 125000                                                                  
      6 lldp 256000                                                                     
      7 sat-control 62500000                                                            
      8 sat-data 250000                                                                 
      9 udld 256000                                                                     
      10 igmp/ipv6-mld 128000                                                           
      11 fip 128000                                                                     
      13 dhcp/ipv6-dhcp 128000                                                          
      14 L3-data-hi 2500000                                                             
      15 L3-data-low 1250000                                                            
      16 L3-front-panel-rdt 250000
      17 isis 128000
      18 cdp 128000
      19 dot1x 125000
      20 pim/ipv6-pim 128000
      21 mgmt/ipv6-mgmt* 1500000
      23 arp/ipv6-nd 128000
      24 rip 1200000
      25 ospf/ipv6-ospf 1200000
      26 eigrp/ipv6-eigrp 1200000
      27 hsrp-vrrp/ipv6-hsrp 128000
      29 bgp/ipv6-bpg 1200000
      30 mcast-first-hop 1200000
      31 ping 8000
      33 mcast-rpf-fail 64000
      35 ucast-fib-miss 8000
      36 glean 128000
      37 msdp 1200000
      38 excption-ttl 8000
      39 exception-same-intf 8000
      40 exception-ip frag
      41 excp/ipv6-excp** 8000
      42 bfd 1200000
      43 mcast-last-hop 64000
      44 wccp 132500
      45 onep-dpss 78125
      46 ecp 800000
      47 vxlan 128000
      48 nvgre 125000
      49 L3-isis 128000
      50 CFSoE-Rel 62500000
      ----------------------------------------------------------------------------------
      
      
      N6K-SPINE3#
      
      
    Advertisements
Categories: Linux
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: