Friday, December 7, 2012

RE: vimdiff: how to calculate "delta" for digits in different lines?


Date: Sat, 8 Dec 2012 00:58:09 -0500
From: songpingemail@gmail.com
To: vim_use@googlegroups.com
CC: rfulbrig@hotmail.com
Subject: Re: vimdiff: how to calculate "delta" for digits in different lines?

On 12/8/2012 12:20 AM, Roy Fulbright wrote:
> > Date: Fri, 7 Dec 2012
      23:50:17 -0500

      > > From: songpingemail@gmail.com

      > > To: vim_use@googlegroups.com

      > > Subject: vimdiff: how to ignore all same lines and show
      ONLY diff lines?

      > >

      > > exports:

      > > by default vimdiff will not only show the diff lines,
      but also show a

      > > couple of same lines to give you a sense of context.

      > > how to suppress all same lines and display only those
      diff lines?

      > >

      > > regards

      > > ping

      > >

      > > --

      > > You received this message from the "vim_use" maillist.

      > > Do not top-post! Type your reply below the text you are
      replying to.

      > > For more information, visit
      http://www.vim.org/maillist.php

      >

      > Add the following line to .vimrc:

      > set diffopt=filler,context:0

      >

      > --

      > You received this message from the "vim_use" maillist.

      > Do not top-post! Type your reply below the text you are
      replying to.

      > For more information, visit http://www.vim.org/maillist.php



thanks Roy (and Tony), that really works!

now I still feel that is a big weak comparing to what I wanted achieve:

say I have these 2 captures from the devices:

  Multicast routing enabled               Multicast routing enabled                                                   
  Use Framed Routes = disabled                    Use Framed Routes = disabled                                                

  In Received Packets 10, Bytes 1000              In Received Packets 15, Bytes 150                                           
    Unicast Packets 10, Bytes 1000                  Unicast Packets 15, Bytes 150                                             
    Multicast Packets 0, Bytes 0                    Multicast Packets 1, Bytes 54                                             
  In Policed Packets 0, Bytes 0                   In Policed Packets 0, Bytes 0                                               
  In Error Packets 0                              In Error Packets 0                                                          
  In Invalid Source Address Packets 0             In Invalid Source Address Packets 0                                         
  In Discarded Packets 0                          In Discarded Packets 0                                                      
  Out Forwarded Packets 100, Bytes 10000          Out Forwarded Packets 150, Bytes 15000                                      
    Unicast Packets 100, Bytes 10000                Unicast Packets 150, Bytes 15000                                          
    Multicast Routed Packets 0, Bytes 0             Multicast Routed Packets 0, Bytes 0                                       
  Out Scheduler Dropped Packets 0, Bytes 0        Out Scheduler Dropped Packets 0, Bytes 0                                    
  Out Policed Packets 0, Bytes 0                  Out Policed Packets 0, Bytes 0                                              
  Out Discarded Packets 0                         Out Discarded Packets 0                                                     



with above solution now I can focus on lines with an increased counter(s).
But quite often I need to manually calculate the "delta" amount of those counters.
so instead of just mark the diff lines, ideally if I can have the 2nd diff window displaying only the delta of those counters whenever there is any difference

29   In Received Packets 10, Bytes 1000          29   In Received Packets 5, Bytes 500         
30     Unicast Packets 10, Bytes 1000                   30     Unicast Packets 5, Bytes 500           
31     Multicast Packets 0, Bytes 0                     31     Multicast Packets 1, Bytes 54          
32 +--  4 lines: In Policed Packets 0, Bytes 0          32 +--  4 lines: In Policed Packets 0, Bytes 0
36   Out Forwarded Packets 100, Bytes 10000             36   Out Forwarded Packets 50, Bytes 5000     
37     Unicast Packets 100, Bytes 10000                 37     Unicast Packets 50, Bytes 5000         

I don't find existing functions/scripts (say script 3745 or 3075) can do that for me,
is that possible
 
 
The easiest way I found to calculate and display the delta of lines that differ is to write a short Perl program:
 
#!/usr/bin/perl
use strict;
use warnings;
my $file1 = "diff1.txt";
my $file2 = "diff2.txt";
open(my $fh1,'<', $file1) or die "Error opening $file1: $!\n";
open(my $fh2,'<', $file2) or die "Error opening $file2: $!\n";
while ( !eof($fh1) && !eof($fh2) ) {
    my $t1 = <$fh1>;
    my $t2 = <$fh2>;
    chomp $t1;
    chomp $t2;
    if ($t1 ne $t2) {
        my ($p1,$b1) = $t1 =~ /(\d+).*?(\d+)/;
        my ($p2,$b2) = $t2 =~ /(\d+).*?(\d+)/;
        my $pdelta = $p2 - $p1;
        my $bdelta = $b2 - $b1;
        print pack("A42",$t1), pack("A42",$t2), "$pdelta  $bdelta\n";
    }
}


No comments:

Post a Comment