トップ «前の日記(2012-07-05) 最新 次の日記(2012-07-08)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2012-07-06 [OSX]beforeafter

以前のプロジェクトでnetstatのログ解析でお世話になっていたコマンドbeforeafter。特に不満はないのだが、著作権の扱いがよく分からない為、ほぼ、同じ動きをするPerlスクリプトを用意してみた。

#!/usr/bin/perl
 
use strict;
use warnings;
 
# netstat -s > netstat.before
# ...
# netstat -s > netstat.after
# beforeafter.pl netstat.before netstat.after
 
if ($#ARGV != 1) {
	print 'Usage: beforeafter.pl before_file after_file', "\n";
	exit 1;
}
 
my $filename1 = $ARGV[0];
my $filename2 = $ARGV[1];
 
open FH1, "<$filename1" or die "error: $filename1: $!\n";
open FH2, "<$filename2" or die "error :$filename2: $!\n";
 
my $c1 = '';
my $c2 = '';
my $d1 = 0;
my $d2 = 0;
my $separator1 = 0;
my $separator2 = 0;
while (not eof FH1) {
	$c1 = getc FH1;
	if ($c1 =~ /\s|\t|:|\(|\n/) {
		print "$c1";
		$separator1 = 1;
	}
	elsif ($c1 =~ /\D/) {
		print "$c1";
		$separator1 = 0;
	}
	else {
		if ($separator1 == 0) {
			print "$c1";
			next;
		}
		my $n1 = $c1;
		$d1 = $n1;
		while (not eof FH1) {
			$c1 = getc FH1;
			if ($c1 =~ /\d/) {
				$n1 = $c1;
				$d1 = (10 * $d1) + $n1;
			}
			else {
				last;
			}
		}
        
		while (not eof FH2) {
			$c2 = getc FH2;
			if ($c2 =~ /\s|\t|:|\(|\n/) {
				$separator2 = 1;
			}
			elsif ($c2 =~ /\D/) {
				$separator2 = 0;
			}
			else {
				if ($separator2 == 0) {
					next;
				}
				my $n2 = $c2;
				$d2 = $n2;
				while (not eof FH2) {
					$c2 = getc FH2;
					if ($c2 =~ /\d/) {
						$n2 = $c2;
						$d2 = (10 * $d2) + $n2;
					}
					else {
						last;
					}
				}
				last;
			}
		}
 
		my $delta = $d2 - $d1;
		print "$delta";
		if (not eof FH1) {
			print "$c1";
		}
	}
}
 
close FH1;
close FH2;
 
# End Of File

使い方は以下のとおり。

$ netstat -s > netstat.before
...何らかの処理をする...
$ netstat -s > netstat.after
$ beforeafter.pl netstat.before netstat.after

_ 関連情報

Annotated Output of netstat -s
Further Discussion of the Statistics

_ 【Cocoa練習帳】

http://www.bitz.co.jp/weblog/
http://ameblo.jp/bitz/(ミラー・サイト)

トップ «前の日記(2012-07-05) 最新 次の日記(2012-07-08)» 編集