Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Perl
Hi,
 
I am a newbie to Perl and have written a code to search for the word "PERFORM" at the start of a line in a file and the corresponding line should be printed in another file. Also I need to store the string after PERFORM in an array
use strict;  
use warnings; 
 
open FILE1, "<Test3.txt" or die "Cannot open File1.txt!";
open FILE2, ">File2.txt" or die "Cannot open File2.txt!";
 

while (my $string=<FILE1>) {
	
	if($string =~ m/^PERFORM\s*/)
	{
	print (FILE2 "$string\n");
	}
	
 
}
close FILE2;
close FILE1;
 
 
But I get output of lines starting with "*" and "END-PERFORM" also.
 
Thanks in advance.
Posted 25-Jul-12 20:42pm
Edited 25-Jul-12 21:13pm
v2
Comments
Chandrasekharan P at 26-Jul-12 4:12am
   
We tried the same program. It works for me. Can you send me some of the lines in your Test3.txt file.
Faez Shingeri at 26-Jul-12 4:35am
   
Here is my Test3.txt
<pre lang="text">
* *****************************************************
* 12/18/02 BPV188 A71921 REL 01/03 *
* REPLACED FCOPYBK WITH F2COPYBK TO *
* IMPROVE THE FILE ACCESS PERFORM ANCE *
* *****************************************************
data to be ignored
data to be ignored
data to be ignored
 
PERFORM op1 op2 op3
data to be ignored
data to be ignored
 
PERFORM op4 op5 op6
 
PERFORM op7 op8 op9
 
PERFORM op0 op0 op0
 
data to be ignored
data to be ignored
 
END-PERFORM
END-PERFORM
</pre>
 

Output file is as below:
 
<pre lang="text">
PERFORM op4 op5 op6
 
PERFORM op7 op8 op9
 
PERFORM op0 op0 op0
 
END-PERFORM
 
END-PERFORM
</pre>
Chandrasekharan P at 26-Jul-12 6:42am
   
I tried it with the data you gave and below is the output that i am getting
 
PERFORM op1 op2 op3
 
PERFORM op4 op5 op6
 
i did not put PERFORM op7 op8 and op9 in the text file.
Faez Shingeri at 26-Jul-12 6:47am
   
:-o Are you sure that you aren't getting this line too..?
* IMPROVE THE FILE ACCESS PERFORM ANCE * //Note the space after PERFORM
 
Could you please share your exact code.?
 
Thanks
Faez Shingeri at 31-Jul-12 0:45am
   
if($string =~ m/^PERFORM\b/) solved the issue.. :)
 
Thanks,

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can begin by reading some regex documentation. The trailing "\s*" does nothing. It is interpreted as zero or more occurances of a whitespace character.
 
Here is a simple to understand regex review ...
 
http://www.cs.tut.fi/~jkorpela/perl/regexp.html[^]
 
As for more specific trouble.. it would help if you included lines that match from your file which you think should not.
  Permalink  
Comments
Faez Shingeri at 31-Jul-12 0:45am
   
Thanks a lot David. The link was brilliant.!
if($string =~ m/^PERFORM\b/) solved the issue..:-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 239
1 Sarvesh Kumar Gupta 185
2 George Jonsson 154
3 ArunRajendra 144
4 Richard MacCutchan 140


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 29 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid