Concord/dci2csv

From s23
Jump to navigation Jump to search

here is a little script i wrote to change DCI rules foles to csv formated files.


#!/bin/perl

use strict;
use warnings;
use Getopt::Long;

Getopt::Long::config qw(no_ignore_case);


my ( $inputFile, $outputAllFields, $searchKey, $outputFields );
our @foundSegments;
{

  $outputFields = 'name,deviceSpeedIn,deviceSpeedOut';
  
  my $needHelp;
  GetOptions( 	'dciInFile=s' 		=> \$inputFile,
		'findField=s'		=> \$searchKey,
		'showFields=s'		=> \$outputFields,
		'listAllFields'		=> \$outputAllFields,
		'help|h|H|?|HELP'  	=> \$needHelp
	);
  die usage() if $needHelp;
}

############################################################
# Data collection
#  using an input file
#  else quit
############################################################

if ($inputFile){  

    findVars($inputFile); 
} else {
die usage() 
} 


############################################################
# Data output 
#	if request for keys print all keys
#	else print results
############################################################

if ($outputAllFields) {
  foreach ( @foundSegments ) {
    foreach (keys %$_) {
      print "$_, ";
    }
  print "\n";
  }

} else {
  foreach my $found (@foundSegments) {
    foreach my $showKey (split /,/, $outputFields) {
      print "$found->{$showKey},";	
    }
  print "\n";
  }
}


sub usage {
  return <<USAGETEXT;
usage: $0 -dciInFile filename  
	-dciInFile filename  is the dci file you want to scan
	the following options are also availble
	[ -findField fieldName ] this is the search key to be used (by default all elements will be returned)
	[ -showFields field names ] feilds to output (default is name,deviceSpeedIn,deviceSpeedOut)
	[ -listAllFields ] list avalible fields
	[ -help] this help text
USAGETEXT
}

sub findVars {
############################################################
# find infomation form Concord's dci files
# usage:
#  findVars("key to search in","value to search for")
#
# output:
#  an aray of Hashes containing all matched segments and keys
############################################################


my ($dciFile, $segmentFieldKey, %segment, $segmentFieldValue, %segmentFields, $nullVar);
my $inElementSection;

# read in dci filename from parent

$dciFile=$_[0] || die qq( FindVars function: Missing dciInfile  value $!);

chomp $dciFile;

open(DCIFILE, "<$dciFile") || die "FindVars function: can not open : $!";


foreach my $line (<DCIFILE>) {
  chomp $line;

  if ( $line =~ /^FN,Elements,/ ) {
    $line =~ s/FN\,Elements\,//g;
    our $i="0";
    foreach (split(/\,/,$line)) {
      $i++;
      $segmentFields{ $i } = $_;
    }
  next;
}; 
  if ( $line =~ /^DS,,Elements,/ ) {$inElementSection=1; next;} #marks start of elements
  if ( $line =~ /^DE/ ) {$inElementSection=0; next;} #marks end of elements
  if ($inElementSection==1) {
    our $ii="0";
    foreach (split(/\,/,$line)) {
      if ($_ =~ /^$/) {$segment{$segmentFields{$ii}}="void";}
      $ii++;
      $segment{$segmentFields{$ii}}=$_;
    }
    push @foundSegments,{%segment};
    next;
  }
}
close DCIFILE;

return (@foundSegments);      # return to main ruteen

}

Back to Concord Index