Solved

read all files in dir and extract specific data and send them to mysql

Posted on 2004-08-02
5
612 Views
Last Modified: 2012-06-22
Hi friends,
I'm new to perl and Mysql and i need the following.if someone can give me the instructions i will go on the ditails myself.
---------------------------------------------------------------------------------
i have some files of format .txt in a directory that each of these files contain many blocks .every block is look like following:
---------------------------------------------------------------------------------
UMTSGSMPLMNCallDataRecord                                  
    mSTerminating                                              
      callPosition                                                03'H
      chargeableDuration                                            0  1 12'BCD
      dateForStartOfCharge                                        040719'H
      exchangeIdentity                                            "RS7G1V0119A11E0"'S
      interruptionTime                                              0  0  0'BCD
      recordSequenceNumber                                        13469605'D
      tariffClass                                                 1'D
      tariffSwitchInd                                             00'H
      timeForStartOfCharge                                          8 50 54'BCD
      timeForStopOfCharge                                           8 52  7'BCD
      outputType                                                  04'H
      switchIdentity                                              0001'H
      calledSubscriberIMSI                                        418400100053236F'TBCD
      calledSubscriberIMEI                                        352966000334860F'TBCD
      timeForTCSeizureCalled                                        8 50 47'BCD
      rFPowerClassCalled                                          04'H
      eosInfo                                                     00'H
      mobileStationRoamingNumber                                  11964758890254'TBCD
      teleServiceCode                                             11'H
      frequencyBandSupported                                      06'H
      mSCIdentification                                           11964750001110'TBCD
      outgoingRoute                                               "ARBSC1O"'S
      calledPartyNumber                                           119647504483236F'TBCD
      networkCallReference                                        A43F0C0001'H
      callingPartyNumber                                          132261097F'TBCD
      incomingRoute                                               "ERBCENI"'S
      presentationAndScreeningIndicator                           10'H
      redirectionCounter                                          00'H
      relatedCallNumber                                           2E033A'H
      callIdentificationNumber                                    3015484'D
      typeOfCallingSubscriber                                     1'D
      radioChannelProperty                                        03'H
      tAC                                                         01020E'H
      subscriptionType                                            00'H
      originForCharging                                           01'H
      chargedParty                                                00'H
      timeFromRegisterSeizureToStartOfCharging                      0  0 11'BCD
      internalCauseAndLoc                                         0003'H
      firstRadioChannelUsed                                       00'H
      firstAssignedSpeechCoderVersion                             01'H
      speechCoderPreferenceList                                   010003'H
      firstCalledLocationInformation                              14F80400650015'H
      lastCalledLocationInformation                               14F80400650015'H
      outgoingAssignedRoute                                       "ARBSC1O"'S
      eMLPPPriorityLevel                                          02'H
      disconnectingParty                                          00'H

-----------------------------------------------------------------------------
i need to write a program in perl to select some specific fields of every block and insert them into mysql data tables.the program should read files in the specified dir sequntialy , open them and extract the specific info into corresponding colomns.i dont know how to read all files one by one and how to for example pick up the amount of incomingRoute that is "ERBCENI"'S  and insert it to appropriate colomn in mysql table.
any help will be appreciated.
Rovan
0
Comment
Question by:ramoo
  • 3
5 Comments
 
LVL 6

Expert Comment

by:holli
ID: 11792734
This is the first part that gets the file contents into a data-structure (an array of hashes, the hashes contain fieldname-value pairs).
Im an not very familiar with DBI and mysql, but if you have problems on that part, just ask again.

#!/usr/bin/perl
use strict;
use Data::Dumper;

my @records;
my %record;

# Put the fields you want here
my @fields =
(
      "timeForStartOfCharge",
      "timeForStopOfCharge",
);

my @files = glob ("your/path/to/file.*");

foreach my $file (@files)
{
  next unless -f $file;
  open FILE, $file or die "Can not open $file $!\n";

  while ( my $line = <FILE>)
  {
    # is it a new block ?
        if ( $line =~ /^UMTSGSMPLMNCallDataRecord/ )
        {
          # after first record
              push @records, {};
            next;
        }

        #try to match the line to a field

        for my $field ( @fields )
        {
              if ( $line =~ /^ +$field +(.+)$/ )
              {
                    $records[-1]->{$field} = $1;
                  next;
              }
        }
  }
  close FILE;

  #last record
  push @records, \%record if %record;
}

print Dumper (\@records);
0
 
LVL 6

Accepted Solution

by:
holli earned 100 total points
ID: 11792758
arrg, correct version:
#!/usr/bin/perl
use strict;
use Data::Dumper;

my @records;
my %record;

# Put the fields you want here
my @fields =
(
     "timeForStartOfCharge",
     "timeForStopOfCharge",
);

my @files = glob ("your/path/to/file.*");

foreach my $file (@files)
{
  next unless -f $file;
  open FILE, $file or die "Can not open $file $!\n";

  while ( my $line = <FILE>)
  {
    # is it a new block ?
       if ( $line =~ /^UMTSGSMPLMNCallDataRecord/ )
       {
          push @records, {};
          next;
       }

       #try to match the line to a field

       for my $field ( @fields )
       {
            if ( $line =~ /^ +$field +(.+)$/ )
            {
               $records[-1]->{$field} = $1;
               next;
            }
       }
  }
  close FILE;
}

print Dumper (\@records);
0
 
LVL 6

Expert Comment

by:holli
ID: 11832067
how´s it going? need further help?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now