#!/usr/bin/perl =pod Script zum Konvertieren Es wird nach Einträgen EXTENSIONNUM,NAME,DATEALLOCATED,ACCOUNTID gesucht und in eine neue Datei geschrieben. Ebenso wird EXTENSIONNUM in eine extra Datei geschrieben Es wird zweimal nach einem Dateinamen Gefragt. Dieser muss ohne endung ".log" angeben werden. Die datei wird im verzeichnis '/home/henrik/workspace' gesucht. =cut ######################### # Modul Definitionen ######################### use strict; use warnings; ########################## # Konfiguration ########################## my $BaseDir='/home/henrik/workspace'; ########################## # Shell leeren ########################## system("clear")==0 or die "Keine Shell\n"; ########################## # Time-Calculation ########################## my ( $Date, $Clock ) = calculate_date_and_clock(); ########################## # DateiName Erfagen ########################## my $InputFile = ''; while(!$InputFile) { $InputFile = ask_for_file('Input File: '); $InputFile = "$BaseDir/$InputFile.log"; last if( -f $InputFile ); print qq(Datei "$InputFile" exstiert nicht! Bitte Erneut eingeben\n); $InputFile = ''; } my @InputData = read_file( $InputFile ); print "Input Linecount : ".scalar( @InputData )."\n"; ########################## # Daten verarbeiten ########################## my @OutputData; my @OutputDataExtensions; while (@InputData) { my $line = shift(@InputData); my ($Ext, $ExtName, $allocDate, $AccountID) = ( '' ) x 4; # Extract the Extension-Number $Ext = $1 if( $line =~ /EXTENSIONNUM=(\d+)/ ); $ExtName = $1 if( $line =~ /NAME=(\d+|\w+\d+)/ ); $allocDate = $1 if( $line =~ /DATEALLOCATED=(\d+-\d+-\d+)/ ); $AccountID = $1 if( $line =~ /ACCOUNTID=(\d+)/ ); next unless $Ext and $ExtName and $allocDate and $AccountID; push(@OutputData,"$ExtName $Ext $AccountID $allocDate\n"); push(@OutputDataExtensions,"$Ext\n"); } print "Output Linecount: ".scalar(@OutputData)."\n"; my $OutputFileName = ask_for_filename('Output File: '); write_file("$BaseDir/$OutputFileName.log", \@OutputData); write_file("$BaseDir/$OutputFileName-Extensions.log", \@OutputDataExtensions); ######################################################################## # Funktionen: ######################################################################## # Aktuelles Datum und Uhrzeit Formatieren sub calculate_date_and_clock { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); my $d = sprintf("%04d-%02d-%02d",$year+1900,$mon+1,$mday); my $c = sprintf("%02d:%02d:%02d",$hour,$min,$sec); return ($d, $c); } # Nach einem Dateinamen Fragen sub ask_for_file { my ( $Msg ) = @_; my $FileName=''; while(!$FileName) { print $Msg; $FileName = ; chomp($FileName); print "Bitte einen Datei Namen angeben!\n" unless $FileName; } return $FileName; } # Eine Datei in ein Array Lesen sub read_file { my ( $File ) = @_; open (my $FH ,'<', $File ) or die "Error Open: $File ($!)"; my @Data = <$FH>; close( $FH ); return @Data; } # Ein Array in eine Datei schreiben sub write_file { my ( $File, $Data ) = @_; open (my $FH ,'>', $File ) or die "Error Open: $File ($!)"; print $FH @$Data; close($FH); return 1; }