PERL: retrieve the data based on regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: retrieve the data based on regular expression
# 1  
Old 03-15-2013
Question PERL: retrieve the data based on regular expression

Hi Friends

i have a code below

sample $text contains the values

Code:
 
test1 PIC X
test1 PIC XX
test1 PIC XXX
test1 PIC X(8)
test1 PIC X(12)
test1 PIC X
test1 X(8)
test1 PIC X VALUE 'N'.

Code:
$text =~ /^\d{6} +(\d{2}) +([\w|\-|\:]+) +PIC +([\d|\w]+)(\((\d+)\)(V([\d|\w]+)| +(COMP\-3).|\.)|( +(COMP\-3).| +(COMP\-3))|\.)/)

the above retreives the
Code:
 
 
test1 PIC X
test1 PIC XX
test1 PIC XXX
test1 PIC X(8)
test1 PIC X(12)
test1 PIC X
test1 X(8)

i am not able to match the last line . i am newbie to perl and ned the regular expression which also captures the last line .

Code:
 
test1 PIC X VALUE 'N'.

Please help
# 2  
Old 03-15-2013
That regex does not match any of lines in the input file.
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-15-2013
Thanks for the reply scrutinizer . Yes sorry please find the exact data below

Code:
 
000800 15 test1 PIC X
000800 15 test1 PIC XX
000800 15 test1 PIC XXX
000800 15 test1 PIC X(8)
000800 15 test1 PIC X(12)
000800 15 test1 PIC X
000800 15 test1 X(8)
000800 15 test1 PIC X VALUE 'N'.

Kindly help
# 4  
Old 03-15-2013
Quote:
Originally Posted by i150371485
...please find the exact data below

Code:
 
000800 15 test1 PIC X
000800 15 test1 PIC XX
000800 15 test1 PIC XXX
000800 15 test1 PIC X(8)
000800 15 test1 PIC X(12)
000800 15 test1 PIC X
000800 15 test1 X(8)
000800 15 test1 PIC X VALUE 'N'.

...
The regex does not match this data either.

Code:
$
$ cat f77
000800 15 test1 PIC X
000800 15 test1 PIC XX
000800 15 test1 PIC XXX
000800 15 test1 PIC X(8)
000800 15 test1 PIC X(12)
000800 15 test1 PIC X
000800 15 test1 X(8)
000800 15 test1 PIC X VALUE 'N'.
$
$
$ perl -lne 'print if /^\d{6} +(\d{2}) +([\w|\-|\:]+) +PIC +([\d|\w]+)(\((\d+)\)(V([\d|\w]+)| +(COMP\-3).|\.)|( +(COMP\-3).| +(COMP\-3))|\.)/' f77
$
$

---------- Post updated at 11:49 AM ---------- Previous update was at 10:58 AM ----------

After a couple of tweaks, a modified regex could be used like so -

Code:
$
$ cat f77
000800 15 test1 PIC X
000800 15 test1 PIC XX
000800 15 test1 PIC XXX
000800 15 test1 PIC X(8)
000800 15 test1 PIC X(12)
000800 15 test1 PIC X
000800 15 test1 X(8)
000800 15 test1 PIC X VALUE 'N'.
$
$ perl -lne 'print if /^\d{6} +(\d\d) +([\w:-]+) +(PIC +)?(\w+)|(\((\d+)\)|(V(\w+)| +(COMP-3).|\.)|( +(COMP-3).| +(COMP-3))|\.)/' f77
000800 15 test1 PIC X
000800 15 test1 PIC XX
000800 15 test1 PIC XXX
000800 15 test1 PIC X(8)
000800 15 test1 PIC X(12)
000800 15 test1 PIC X
000800 15 test1 X(8)
000800 15 test1 PIC X VALUE 'N'.
$
$

There are many components in the regex that don't really help in matching the posted data. Maybe you are using the regex elsewhere, or maybe your data is much more diverse. If that's not the case, you could use a simpler regex that matches just the posted data. Secondly, the parentheses seem redundant here, unless you are using the captured groups.

Keeping these two points in mind, a shorter regex is as follows:

Code:
$
$ perl -lne 'print if /^\d{6}\s+\d\d\s+\w+\s+(PIC\s+)?X+(\(\d+\)| VALUE.*)?/' f77
000800 15 test1 PIC X
000800 15 test1 PIC XX
000800 15 test1 PIC XXX
000800 15 test1 PIC X(8)
000800 15 test1 PIC X(12)
000800 15 test1 PIC X
000800 15 test1 X(8)
000800 15 test1 PIC X VALUE 'N'.
$
$

This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 03-18-2013
@durden_tyler : Thanks for the information Smilie it helps me and will try to modify the reg ex with respect to my requirements .. thanks again Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

2. Shell Programming and Scripting

Perl regular expression help!

Hi I am doing something basic like... if ($stringvariable =~ /have not typed/) I have a little problem because the 'not' in the expression gets highlighted as a kind of a '!'..what am I supposed to do in this situation? Thank you ---------- Post updated at 03:24 PM ----------... (1 Reply)
Discussion started by: vas28r13
1 Replies

3. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

4. Shell Programming and Scripting

Perl regular expression and %

Could you help me with this please. This regular expression seems to match for the wrong input #!/usr/bin/perl my $inputtext = "W1a$%XXX"; if($inputtext =~ m/+X+/) { print "matches\n"; } The problem seems to be %. if inputtext is W1a$XXX, the regex doesnot match.... (5 Replies)
Discussion started by: suppandi7
5 Replies

5. Shell Programming and Scripting

Perl Regular Expression

Hello, I am trying to use perl LWP module to read and get a specfic URL page. The issue is that the URL ends with the data and time and time is not consistent it changes all the time. if anyone could help me how to write a regular expressin that would work in the LWP::UserAgent get function to... (0 Replies)
Discussion started by: bataf
0 Replies

6. Shell Programming and Scripting

Need perl regular expression

Hi, I am looking for a Perl regular expression to match the below pattern of a java script file. var so = object.device.load('camera','value'); I want to grep out such lines present in the *.js files. The conditions are: a) the line may start with blank space(s) b) always the... (3 Replies)
Discussion started by: royalibrahim
3 Replies

7. Shell Programming and Scripting

perl regular expression

Dear all, I have a simple issue on a perl regular expression. I want to get the characters in red from the next lines : POWER_key LEFT_key RIGHT_key OK_key DOWN_key and so on... Thanks in advance for reply. Ludo (1 Reply)
Discussion started by: lsaas
1 Replies

8. Shell Programming and Scripting

regular expression in perl

hi, i want to extract the sessionID from this line. QnA Session Id : here the output should be-- QnA_SessionID=128589 Thanks NT (3 Replies)
Discussion started by: namishtiwari
3 Replies

9. Shell Programming and Scripting

perl regular expression

letz say that my file has 7 records with only one field. So my file has: 11111111 000000000000000 1111 aaaabbbccc 1111111222000000 aaaaaaaa zz All i need is: 1. when the field has a repetition of the same instance(a-z or 0-9), i would consideer it to be invalid.... (1 Reply)
Discussion started by: helengoldman
1 Replies

10. Shell Programming and Scripting

Regular expression help in perl

Hi all, I am trying to match a multi line string and return the matching string in one line. Here is the perl code that I wrote: #!/usr/bin/perl my $str='<title>My title</title>'; if ($str =~ /(<title>)(+)(<\/title>)/ ){ print "$2\n"; } It returns : My title I want the... (3 Replies)
Discussion started by: sdubey
3 Replies
Login or Register to Ask a Question