Perl Programming for Splitting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Programming for Splitting
# 1  
Old 04-02-2009
Perl Programming for Splitting

Hi,

I am extracting SQL queries into a file and the file is as follows

*********************************************************
select BatchKey ,restartStatus ,batchContextBuffer ,batchPgmId ,StartKey ,
EndKey ,Mcbatchcontrol_ver
from
qsecminload.Mcbatchcontrol_t where RefId = :1
*********************************************************
SELECT DEO_ENTITY_ID, DEO_EVNT_ID, DEO_DPT_ID, DPT_EVNT_TYP,
DPT_EVNT_GRP, DPT_SEC_ID, DPT_SEC_TYP, DPT_SEC_SB_TYP, DEO_OPTN_SEQ_N,
DEO_OPTN_TYP, DEO_DPOT_EVNT_OPTN.OU_ID, DEO_DPOT_EVNT_OPTN.PART_ID,
DEO_DPOT_EVNT_OPTN.USERID, DPT_EVNT_CLSFCTN
FROM
DPT_DPOT_EVNT_DTLS, EDD_EVNT_DT_DTLS, DEO_DPOT_EVNT_OPTN,
SBD_SYS_BSNS_DTS WHERE DPT_ENTITY_ID = EDD_ENTITY_ID AND
EDD_ENTITY_ID = DEO_ENTITY_ID AND SBD_ENTITY_ID = DEO_ENTITY_ID ORDER BY DEO_EVNT_ID ,
DEO_ENTITY_ID , DEO_DPT_ID , DEO_OPTN_SEQ_N
**********************************************************


I have to identify the pattern (from or FROM) and copy the contents till the Pattern (where or WHERE). Next I have to split the content copied based on comma(,) then count the number of variables.

The out put will have to be as follows
**********************************************************
qsecminload.Mcbatchcontrol_t
count=1
**********************************************************
DPT_DPOT_EVNT_DTLS, EDD_EVNT_DT_DTLS, DEO_DPOT_EVNT_OPTN,
SBD_SYS_BSNS_DTS
count=4

Please help. Thanks in advance
# 2  
Old 04-02-2009
Please post what code you have written so far to solve your programs requirements.
# 3  
Old 04-03-2009
# Program to get the Query, Parse count, Execute count, Fetch count, Total cpu time and Total elapsed time.

# Begin Prompt for User Input .. Input Trace File

$inputFile = &promptUser("\n\t\t Enter the TraceFile Name");


#Open input and output files and create File Handles

open(INFILE,"<$inputFile") or die " Input File opening ERROR :: $!";
$opFile = $inputFile.".xls";
open(OUTFILE,">$opFile") or die " Output File opening ERROR :: $!";

# Write the Headings to the file
print OUTFILE "SQL Query and the Indexes used in the Query is printed in the output_log file \n\n";
#print OUTFILE "===================================================================\n";
#print OUTFILE "SQL QUERY ~PARSE Count ~EXECUTE Count ~FETCH Count ~TOTAL CPU TIME ~TOTAL ELAPSED TIME ~FullTableScan\n";
# initializations

$SQLLINE="";
$CUTLINE="";
$CUTLINE1="";
$FLAG=0;
$SUFLAG=0;
$Indexflag=0;
$MyFlag=0;
$Rows=0;
$Blocks=0;
$Result=0;
$Zero=0;
$Level=10;
#
# Working on the input file One Line at a Time !!! Read from given I/P file Line by Line.
foreach $actual_line (<INFILE>)
{
# To get the PLAN !!!

# if line is a sql statement change the flag status
# and copy the line to a variable


if( $actual_line =~ m/^SELECT/ || $actual_line =~ m/^select/ || $actual_line =~ m/^update/ || $actual_line =~ m/^UPDATE/) {
$FLAG=1;
$SUFLAG=1;
#print OUTFILE "------------------------------------------------------------------------------- \n\n";
chomp($actual_line);
$SQLLINE=$actual_line;
}

#change the flag status when complete sql statement is read

elsif($actual_line =~ m/^call/){
#print "call";
$FLAG=0;
print OUTFILE "***************************************************************************** \n";
print OUTFILE "$SQLLINE" ;
$CUTLINE=$SQLLINE;
@charArray = split(/FROM/,$CUTLINE);
$CUTLINE1=@charArray[1];
print OUTFILE "$CUTLINE1" ;

$SQLLINE="";
next;
}

# read the complete sql statement line by line and join them together

elsif($FLAG==1) {
chomp($actual_line);
$line=$actual_line;
$temp=$SQLLINE;
$SQLLINE=$temp.$line;
next;
}
# Above code Completes the program for copying the SQL Query.


else{
next;
}



}


##
# Close all file handles !!!
##
close(INFILE) or die " INFILE CLOSING ERROR : $!";
close(OUTFILE) or die " OUTFILE CLOSING ERROR : $!";

#
# Display Console message to user ..

print "\t\tOUTPUT printed to File : $opFile \n";



# Prompting the usr to enter an Input file or accept a default I/P File ...
#
#

sub promptUser
{

local($promptString,$defaultValue) = @_;

if ($defaultValue)
{
print $promptString, "[", $defaultValue, "]: ";
}
else
{
print $promptString, ": ";
}

$| = 1; # force a flush after our print
$_ = <STDIN>; # get the input from STDIN (presumably the keyboard)

chomp;

if ("$defaultValue")
{
return $_ ? $_ : $defaultValue; # return $_ if it has a value
}
else
{
return $_;
}
}



This is the code that I have created. The problem comes as it is splitting for FROM but it is not splitting for from.
Also need to continue with the logic
# 4  
Old 04-03-2009
Try this:

Code:
@charArray = split(/FROM|from/, $CUTLINE);

# 5  
Old 04-03-2009
This might be more accurate:

@charArray = split(/\bfrom\b/i, $CUTLINE);

otherwise 'from' could be a substring of another word and will split the file incorrectly. 'i' makes the match case-insensitive so 'FROM' and 'from' will match.
# 6  
Old 04-06-2009
Thanks Kevin.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting a file and creating new files using Perl script

Hi All, I am new to Scripting language. I want to split a file and create several subfiles using Perl script. Example : File format : Sourcename ID Date Nbr SU IMYFDJ 9/17/2012 5552159976555 SU BWZMIG 9/14/2012 1952257857887 AR PEHQDF 11/26/2012 ... (13 Replies)
Discussion started by: Deepak9870
13 Replies

2. Shell Programming and Scripting

perl : splitting the data into 2 different variables

I have a perl variable which contains the below value. $var1 = "2% / 51%" Now I would like to split the data into 2 different variables. For example $part1 = 2 $part2 = 51 Could anyone please help me in this regard ? Regards, GS (4 Replies)
Discussion started by: giridhar276
4 Replies

3. Shell Programming and Scripting

Perl programming help

I am trying to make a simple perl program that reads 20 characters upstream from the codon ATG in a given sequence. The following is what I have. I just dont know how to make the program read 20 characters upstream from the ATG codon. print "\nThis program will read 20 characters upstream... (1 Reply)
Discussion started by: patiencenpray
1 Replies

4. Shell Programming and Scripting

Splitting a file into several smaller files using perl

Hi, I'm trying to split a large file into several smaller files the script will have two input arguments argument1=filename and argument2=no of files to be split. In my large input file I have a header followed by 100009 records The first line is a header; I want this header in all my... (9 Replies)
Discussion started by: ramky79
9 Replies

5. Shell Programming and Scripting

perl programming

how to link the linux files in perl on the local webpage ???? suppose we have some results and want to get them published on the local webpage of our internal site. how this can be done using HTML and perl together , so that the results are published directly on the webpage. thanks kullu (0 Replies)
Discussion started by: kullu
0 Replies

6. Shell Programming and Scripting

perl, splitting out specific parts of the string

Hi there, I have an output from a command like this # ypcat -k netgroup.byuser| grep steven steven.* users_main,users_sysadmin,users_global,users_backup_team and wanted to pull the 'users' netgroups returned into a perl array, that will look like this users_main... (2 Replies)
Discussion started by: rethink
2 Replies

7. Shell Programming and Scripting

PERL need help splitting argument

If i have a script name.pl I run it like name.pl -v file.txt -t ext2 -u user -j how can I edit the array @ARGV so when my script calls $ARGV = -v file.txt $ARGV = -j (2 Replies)
Discussion started by: 3junior
2 Replies

8. Shell Programming and Scripting

Perl - Need help on splitting string

Let's say I have a very long string with no spaces but just words stored in $very_long_string. $very_long_string = "aaaaaaaaaaabbbbbbbbbbbccccccccccccdddddddddddd"; I can do this to split the string into 1 character each and store them in an array: @myArray = split(//, $very_long_string); ... (3 Replies)
Discussion started by: teiji
3 Replies

9. UNIX for Dummies Questions & Answers

PERL - DB programming

Hi friends, What are the possible ways to connect to DB2 database from Perl (on unix). I need to connect to DB2 and get records for further processing. Can you please suggest the best possible way. I heard about DBI/DBD, if you have some sample scripts please post them too. Thanks in advance. (3 Replies)
Discussion started by: satguyz
3 Replies

10. Shell Programming and Scripting

splitting on dot in perl

I am trying to split input that looks like ,2005-09-12 01:45:00.000000,2005-09-12 01:48:18.000000, I want to split on the dot . What I am using is ($ev_time,$rol)=split(/\./),$inputfile; This does not recognize the dot as what I want to split on. (2 Replies)
Discussion started by: reggiej
2 Replies
Login or Register to Ask a Question