greatshots has asked for the wisdom of the Perl Monks concerning the following question:

monks,

#!/usr/bin/perl $file = shift; open ( FH , "$file" ) or die "$!"; #open ( NT , ">new_table" ) or die "$!"; open ( OUT , ">>out" ) or die "$!"; $old_started = 0; print OUT "New_schema | Old_schema\n"; while ( <FH> ) { chomp; $line = $_; next if ( $line =~ /^--/ or $line =~ /^$/ or $line =~ /^\s+$/); if ( $line =~ /^INSERT INTO/i ) { $line =~ /INSERT INTO (.*)\((.*),/; $new_table = $1; $column = $2; $column =~ s/,//g; push @new_columns, $column; } while ( 1 ) { chomp($line = <FH>); $line =~ s/,//g; $line =~ s/\)//g; if ( $line =~ /^SELECT/ || $old_started == 1 ) { if ( $line =~ /^FROM/ ) { $line =~ /FROM (.*);/; $old_table = $1; $old_started = 0; $index = 0; while ( $new_columns[$index] ) { print OUT "$new_table.$new_columns[$index] | $old_table.$old_c +olumns[$index]\n"; $index++; } $index = 0; #print "$new_table => @new_columns : $#new_columns\n"; #print "$old_table => @old_columns : $#old_columns\n"; last; } push @old_columns, $line if ( $line !~ /^SELECT/); $old_started = 1; next; } push @new_columns,$line; } }
Inout would be something like this
-- 2) 206: The specified table (xxxxx) is not in the database -- 111: ISAM error: no record found. -- Solution : You have to install the SGN3. INSERT INTO nokia_scp_address_camel_1_raw(nc_id, scp_address_id, day, sgsncamelactivepdpcontexts, sgsncamelactivesmses, sgsncamelcapdialforpdpcontexts, sgsncamelcapdialoguesforsmses, sgsncamelcapfailwithpdpcontext) SELECT nc_id, scp_address_id, day, sgsncamelactivepdpcontexts, sgsncamelactivesmses, sgsncamelcapdialforpdpcontexts, sgsncamelcapdialoguesforsmses, sgsncamelcapfailwithpdpcontext FROM nokia_scp_address_camel_raw;
From the above input the script produce the output as
nokia_scp_address_camel_1_raw.nc_id + nokia_scp_address_camel_raw.nc_id nokia_scp_address_camel_1_raw.scp_address_id nokia_scp_ +address_camel_raw.scp_address_id nokia_scp_address_camel_1_raw.day nokia_scp_address +_camel_raw.day nokia_scp_address_camel_1_raw.sgsncamelactivepdpcontexts nokia_ +scp_address_camel_raw.sgsncamelactivepdpcontexts nokia_scp_address_camel_1_raw.sgsncamelactivesmses nokia_sc +p_address_camel_raw.sgsncamelactivesmses nokia_scp_address_camel_1_raw.sgsncamelcapdialforpdpcontexts no +kia_scp_address_camel_raw.sgsncamelcapdialforpdpcontexts nokia_scp_address_camel_1_raw.sgsncamelcapdialoguesforsmses nok +ia_scp_address_camel_raw.sgsncamelcapdialoguesforsmses nokia_scp_address_camel_1_raw.sgsncamelcapfailwithpdpcontext no +kia_scp_address_camel_raw.sgsncamelcapfailwithpdpcontext
Is ther any simply way will i be able to acheive the task.

Replies are listed 'Best First'.
Re: need a intelligence way of parsing the file
by BrowserUk (Patriarch) on Oct 30, 2006 at 09:49 UTC

    #! perl -slw use strict; $/ = ";\n"; ## Paragraph mode Blocks that end with ';' ## Assumes there are no semicolons at the end of lines in the comments +. while( <DATA> ) { m[ .* ## skip stuff before the INSERT ## Capture the dest table name and the entire dest fields list INSERT \s+ INTO \s+ ( \S+ ) \s* \( \s* ( [^)]+ ) \s* \) \s* ## Capture the entire src fields list & source table name SELECT \s+ ( .+? ) \s+ FROM \s+ ( .+? ) \s* ; ]sx or warn "'$_' did not match; ignored" and next; my( $dTable, $dFields, $sFields, $sTable ) = ( $1, $2, $3, $4 ); my @dFields = split '\s*,\s*', $dFields; my @sFields = split '\s*,\s*', $sFields; printf "%-60s %-60s\n", $dTable . '.' . $dFields[ $_ ], $sTable . '.' . $sFields[ $_ ] for 0.. $#dFields; print "\n"; }

    Output from your example input:

    C:\test>581226.pl nokia_scp_address_camel_1_raw.nc_id nokia_scp +_address_camel_raw.nc_id nokia_scp_address_camel_1_raw.scp_address_id nokia_scp +_address_camel_raw.scp_address_id nokia_scp_address_camel_1_raw.day nokia_scp +_address_camel_raw.day nokia_scp_address_camel_1_raw.sgsncamelactivepdpcontexts nokia_scp +_address_camel_raw.sgsncamelactivepdpcontexts nokia_scp_address_camel_1_raw.sgsncamelactivesmses nokia_scp +_address_camel_raw.sgsncamelactivesmses nokia_scp_address_camel_1_raw.sgsncamelcapdialforpdpcontexts nokia_scp +_address_camel_raw.sgsncamelcapdialforpdpcontexts nokia_scp_address_camel_1_raw.sgsncamelcapdialoguesforsmses nokia_scp +_address_camel_raw.sgsncamelcapdialoguesforsmses nokia_scp_address_camel_1_raw.sgsncamelcapfailwithpdpcontext nokia_scp +_address_camel_raw.sgsncamelcapfailwithpdpcontext

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: need a intelligence way of parsing the file
by shmem (Chancellor) on Oct 30, 2006 at 09:34 UTC
    Is ther any simply way will i be able to acheive the task.
    Which task? You didn't describe that. Am I supossed to guess the task from your script, its input and its output? Do you want a code review? Are you seeking a simpler solution than yours?

    What exactly is the question here?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      sorry for not mentioning the task properly. I want someone to review my code. because the script is running against a huge amount of data.