Need Suggestions to improve Perl script for checking malformed braces/brackets


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Suggestions to improve Perl script for checking malformed braces/brackets
# 1  
Old 07-28-2011
Need Suggestions to improve Perl script for checking malformed braces/brackets

Hi all,

I've written a Perl script below that check and report for malformed braces. I have a UNIX ksh version and it took a couple of minutes to run on a 10000+ lines. With the Perl version it only took about 20 seconds so that is enough incentive for me to go Perl not to mention that I need to run this on a Windows as well later.

The script and the file testfile01x.ora sample file are attached. I've renamed the testfile01x.ora to testfile01x.txt as the forum does not accept .ora extension

Sample output of the run as below:

Code:
./check_malform.pl testfile01x.ora
Checking -> START = 1 :: END = 13 :: TNS = prod10.checks.com.ph = = MALFORMed !!!
Checking -> START = 14 :: END = 26 :: TNS = prod11.checks.com.ph = = OKAY
Checking -> START = 27 :: END = 39 :: TNS = prod20.checks.com.ph = = OKAY
Checking -> START = 40 :: END = 52 :: TNS = devd11.checks.com.ph = = MALFORMed !!!
Checking -> START = 53 :: END = 65 :: TNS = tstt11.checks.com.ph =  = OKAY
Checking -> START = 66 :: END = 78 :: TNS = devd1.checks.com.ph =  = OKAY
Checking -> START = 79 :: END = 87 :: TNS = quald_app.checks.com.ph = = OKAY
START  = Thu Jul 28 16:24:02 NZST 2011
FINISH = Thu Jul 28 16:24:02 NZST 2011

I want to know if anyone have any advise on how to "improve" the code somehow? The code works as it is the way I want it to but maybe there is a better way around it.

A short explanation of what the script does is as below:
- The script is run as check_malform.pl testfile01x.ora. testfile01x.ora is the file to check for malformed braces
- The script remove all lines that begin with # and all blank lines
- It selects all lines that begins with an alphabetic character and place its start and end into a tmp file
- It reads the tmp file and select some portions of the file and place these lines in another tmp file.
- It checks the second tmp file and count the number of open ( and close ) braces
- If the number of open and close braces do not match, then it is reported as MALFORMED

Main "improvement" that am hoping for is being able to read specific lines of a file just like the UNIX sed command. Removing the blank lines and comments seems unnecessary but did it just the same just to rid the files of things that am not interested in.

I may want to expand the script at some stage to cover other malformed characters, for example, < and >, { and }, [ and ] etc. but for now, am happy with just ( and ).

Tried a bracket checker from [Perl] bracket checker - Pastebin.com but it is giving me errors as below when I ran it:

Code:
./123.pl testfile01x.ora
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xab) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xab) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xab) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xab) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xab) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xab) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xbb) at ./123.pl line 8.
Sequence (?|...) not recognized before HERE mark in regex m/(?| << HERE
    (\()(?&matched)([\}\]..?>??]|$) |
    (\{)(?&matched)([\)\]..?>??]|$) |
    (\[)(?&matched)([\)\}..?>??]|$) |
    (.)(?&matched)([\)\}\].?>??]|$) |
    (.)(?&matched)([\)\}\].?>??]|$) |
    ()(?&matched)([\)\}\]..?>??]|$) |
    (?)(?&matched)([\)\}\]..>??]|$) |
    (<)(?&matched)([\)\}\]..???]|$) |
    ()(?&matched)([\)\}\]..?>??]|$) |
    (?)(?&matched)([\)\}\]..?>?]|$) |
    (?)(?&matched)([\)\}\]..?>?]|$) | at ./123.pl line 34.

Any suggestion will be much appreciated. Thanks in advance.

Last edited by pludi; 07-28-2011 at 04:39 AM..
# 2  
Old 07-28-2011
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Improve script

Gents, Is there the possibility to improve this script to be able to have same output information. I did this script, but I believe there is a very short code to get same output here my script awk -F, '{if($10>0 && $10<=15) print $6}' tmp1 | sort -k1n | awk '{a++} END { for (n in a )... (23 Replies)
Discussion started by: jiam912
23 Replies

2. Shell Programming and Scripting

How to improve an script?

Gents. I have 2 different scripts for the same purpose: raw2csv_1 Script raw2csv_1 finish the process in less that 1 minute raw2csv_2 Script raw2csv_2 finish the process in more that 6 minutes. Can you please check if there is any option to improve the raw2csv_2. To finish the job... (4 Replies)
Discussion started by: jiam912
4 Replies

3. AIX

[Tip] /dev filling because of malformed IBM script

There is some IBM script out there, which contains a hidden syntax error. I am not sure which script it is (we are still investigating), but most of my HACMP systems (up to version 6.1) showed the symptom and i suspect the source to be in HACMP. Have a look at your system. Probably someone at... (1 Reply)
Discussion started by: bakunin
1 Replies

4. Shell Programming and Scripting

perl: How to improve with minimal validation of its input??

The Code: #!/usr/bin/perl use strict; use warnings; print "Please enter numbers, separated by commas: "; my $data=<STDIN>; chomp $data; my @dataset=split(/,/, $data); my $sum = 0; foreach my $num (@dataset) { $sum += $num; } my $total_nums = scalar(@dataset); my $mean =... (1 Reply)
Discussion started by: 300zxmuro
1 Replies

5. Shell Programming and Scripting

How to improve the performance of parsers in Perl?

Hi, I have around one lakh records. I have used XML for the creation of the data. I have used these 2 Perl modules. use XML::DOM; use XML::LibXML; The data will loo like this and most it is textual entries. <eid>19000</eid> <einfo>This is the ..........</einfo> ......... (3 Replies)
Discussion started by: vanitham
3 Replies

6. Shell Programming and Scripting

How to substitute brackets in the beginning of string in perl?

Hi, I have a string like this user can specify different query sets that is why "or" is mentioned: $string="]("; or $string="](("; or $string="]((("; or $string="]((((("; (1 Reply)
Discussion started by: vanitham
1 Replies

7. Shell Programming and Scripting

Any way to improve performance of this script

I have a data file of 2 gig I need to do all these, but its taking hours, any where i can improve performance, thanks a lot #!/usr/bin/ksh echo TIMESTAMP="$(date +'_%y-%m-%d.%H-%M-%S')" function showHelp { cat << EOF >&2 syntax extreme.sh FILENAME Specify filename to parse EOF... (3 Replies)
Discussion started by: sirababu
3 Replies

8. Shell Programming and Scripting

how to find matching braces using sed or in shell script

hi, I want to print all the lines between the matching braces. For example,the file contains like the below. asdfsdf fsdfsd WO{ w1{ ada ... (3 Replies)
Discussion started by: Boopesh
3 Replies

9. Shell Programming and Scripting

Can I improve this script ???

Hi all, Still a newbie and learning as I go ... as you do :) Have created this script to report on disc usage and I've just included the ChkSpace function this morning. It's the first time I've read a file (line-by-bloody-line) and would like to know if I can improve this script ? FYI - I... (11 Replies)
Discussion started by: Cameron
11 Replies
Login or Register to Ask a Question