regexsedgrep

Parse IP and Download-Total from mikrotik


I wanna extract IP and download-total from mikrotik command /queue simple print stat
Here's some example :

0    name="101" target=192.168.10.101/32 rate=0bps/0bps total-rate=0bps 
  packet-rate=0/0 total-packet-rate=0 queued-bytes=0/0 
  total-queued-bytes=0 queued-packets=0/0 total-queued-packets=0 
  bytes=17574842/389197663 total-bytes=0 packets=191226/308561 
  total-packets=0 dropped=9/5899 total-dropped=0 

1    name="102" target=192.168.10.102/32 rate=0bps/0bps total-rate=0bps 
  packet-rate=0/0 total-packet-rate=0 queued-bytes=0/0 
  total-queued-bytes=0 queued-packets=0/0 total-queued-packets=0 
  bytes=65593392/183786457 total-bytes=0 packets=163260/166022 
  total-packets=0 dropped=175/2403 total-dropped=0 

2    name="103" target=192.168.10.103/32 rate=0bps/0bps total-rate=0bps 
  packet-rate=0/0 total-packet-rate=0 queued-bytes=0/0 
  total-queued-bytes=0 queued-packets=0/0 total-queued-packets=0 
  bytes=3263234/67407044 total-bytes=0 packets=41437/52602 
  total-packets=0 dropped=0/546 total-dropped=0

All that I need is :

192.168.10.101 389197663  
192.168.10.102 183786457
192.168.10.103 67407044  

But I get

target=192.168.10.101/32
bytes=17574842/389197663
target=192.168.10.102/32
bytes=65593392/183786457
target=192.168.10.103/32
bytes=3263234/67407044

I try it with grep -oP 'target=.*?\ |[^\-]bytes=.*?\ ' | sed 's/^ //g'. So, how can I parse it? Sorry for bad english..


Solution

  • Just continue your line of parsing with another pipes (most easy way i think)

    grep -oP 'target=.*?\ |[^\-]bytes=.*?\ ' file | sed 's/^ //g' | sed -r 's/target=([^/]*)[/].*/\1/; s/bytes=[^/]*[/]//' | sed 'N; s/\n/ /'
    

    output

    192.168.10.101 389197663 
    192.168.10.102 183786457 
    192.168.10.103 67407044