Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count commas from input file for output file

Status
Not open for further replies.

netman4u

Technical User
Joined
Mar 16, 2005
Messages
176
Location
US
Hello all,

I am writing a program that reads an input file like this:

Code:
#  Vendor	Commands
#
Juniper&	show int, , , , , , , , ,
Cisco&		, ,sho bgp, , , , , , ,
Junk&		, , , , ,sho nothing, , , ,
Redback&	, , ,sho tunnel pear, , , , , ,

As you can see the "Commands" column is a csv. My program reads the vendor type and runs the appropriate commands it finds for that vendor, parses the command and generated one number and then writes the results to an output file.

My output file needs to be in this format:

Code:
IP		Time Stamp	metric1,metric2,metric3,metric4,metric5,metric6,metricX

10.10.10.10	17:20:16	 , , ,35, ,67,
10.10.10.11	17:21:01	10, , ,56, , ,

I already have the ip and timestamp so no problem there. What I am looking for is a way to output commas in a way that corresponds to the number of input commas and the placement of the input commands. So for the first and second entry in my input file my output would be:

Code:
10.10.10.10		17:20:16	 35, , , , , , , , ,
10.10.10.11		17:21:01	 , ,58, , , , , , ,

So as you can see if the input has a command followed by 9 commas, the output would be my number followed by 9 commas.

Hers is the relevant code I have so far:

Code:
#
# Read config file, load a vendor => CLI hash and count the commas. CODE BELOW TESTED!
#
my @count;
my %cmd;
my $count;
my @tmp;
foreach(@cfg) {
	next if /^#/; 
	@tmp = split(/&/,$_);
    $tmp[1] =~ s/^\s+//;
    $tmp[1] =~ s/\s+$//;
	$cmd{$tmp[0]} = $tmp[1];
	@count = split(/,/,$tmp[1]);
	$count = scalar(@count); # number of commas always the same for output
}
#
# Query the InfoVista DB for devices to run CLI commands on.
#
my %db_results = VistaQuery();
#print "$count\n";
#while ( my($k,$v) = each %db_results) {
#    print "$k => $v\n";
#}
##############################################################################################
#
#   Main Loop
#
##############################################################################################
#
#  Loop through the IPs returned from the InfoVits query and SSH to each ip and run 
#  the appropriate CLI commands for the vendor. 
#
#my %db_results = ('ip' => '205.152.201.22', 'vendor' => 'Redback');
while(my($ip,$vendor) = each %db_results) {
	my @time_stamp;
	next unless ($vendor);
	print "$ip => $vendor\n";
#	my $cli = $cmd{$vendor};
#	my $t = new Net::Telnet (Timeout => 10,
#                          Prompt => '/(.*)\#/');
#    $t->open($ip);
#    $t->login($user, $pass);
#    my @lines = $t->cmd($cli);
#    print @lines;
#	my $vendor = "Redback";
	my @cmds = split (/,/,$cmd{$vendor});
	print "commands to be run are @cmds\n";
#   my $ssh = Net::SSH::Perl->new($ip);
#   $ssh->login($cliuser, $clipass);
#	my @results;
	foreach my $cmd1 (@cmds) {
		next unless ($cmd1 =~ /\w+/);
#		sshopen2("$cliuser\@$ip", *READER, *WRITER, "$cmd1") || die "$!";
#
#		while (<READER>) {
#		  chomp();
#		  print "$_\n";
#		}
#
#		close(READER);
#		close(WRITER);
#
#	    my($stdout1, $stderr1, $exit1) = $ssh->cmd($cmd1);
#		if ($debug == 1) {
#			if ($stderr) {print LOG "Error Code: $stderr\nExit Code:$exit\n\n";}
#			else {print LOG "No error messages for $cmd1\n\n";}
#			my @time_stamp = GetTime();
#		push(@results, $stdout1); # move this into the loop?
		if ($vendor == "Foundry") {
			foreach  (@results) {
				chomp;
				next unless /^Maximum/;
				/^.*IDBs\s+(\d+)\..*use\s+(\d+)/;
				$foundry{$1} = $2; 
				print OUT 
			}
		}elsif ($vendor == "Cisco") {
		}
	}
}
	#  Parse the cli output array @results here and print to output file.
	#
#	}
} # End Main Loop
Much is commented ot as it is a work in progress.

So I have the number of commas in a scalar $count, but I need a way to count the commas before and after the command and then output them in the ouput file. Also the number of commas (columns) will always be the same in both input and output file.





If at first you don't succeed, don't try skydiving.
 
Add a third parameter of -1 to the split function.
Code:
    my @cmds = split (/,/,$cmd{$vendor},-1);


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top