By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,253 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,253 IT Pros & Developers. It's quick & easy.

how to search a pattern occured more than once in line

P: 4
Hi all,

I have one input file "pick no.txt" from which i have to find perticuler pattern and print it in to output file "Numbers.txt".

The input file is

1."pick no.txt"


Number:
Number-1999-1011
----------------------------------------
Number:
Number-1999-0696
----------------------------------------
Number:
Number-1999-0833,Number-2004-0786, Number-2004-0747, Number-2004-0751, Number-2004-0748, Number-2004-0809
----------------------------------------
Number:
Number-2000-1209
----------------------------------------

The perl script i made for this is..

#!/usr/bin/perl

open (RD, "pick no.txt") or die"Could not open file";
@Read = <RD>; close (RD);

open (WR1, ">>Numbers.txt") or die"Could not open file";

for ($i=0; $i<= $#Read; $i++)
{
if($Read[$i] =~ /^Number:/ and $Read[$i+1] =~ /(\w\w\w\w\w\w-\d\d\d\d-\d\d\d\d)/)
{

$Number = $1;
$Number1 = substr ($Number, 7, 9);
print "$Number1\n";
print WR1"$Number1\n";
}
}
-------------------------------------------------------------------

The output of the script is Numbers.txt..

1999-1011
1999-0696
1999-0833
2000-1209

The problem with this is it is not useful to search pattern occurance more than once in the same line. The numers after 1999-0833 is not shown in thw output.
Any suggestions for this?

--Mahesh
Jan 29 '08 #1
Share this Question
Share on Google+
8 Replies


P: 36
Even you can use split function to findout the number of occurance of that pattern in script.

shafi
Jan 29 '08 #2

mehj123
P: 55
Hi all,

I have one input file "pick no.txt" from which i have to find perticuler pattern and print it in to output file "Numbers.txt".

The input file is

1."pick no.txt"


Number:
Number-1999-1011
----------------------------------------
Number:
Number-1999-0696
----------------------------------------
Number:
Number-1999-0833,Number-2004-0786, Number-2004-0747, Number-2004-0751, Number-2004-0748, Number-2004-0809
----------------------------------------
Number:
Number-2000-1209
----------------------------------------

The perl script i made for this is..

#!/usr/bin/perl

open (RD, "pick no.txt") or die"Could not open file";
@Read = <RD>; close (RD);

open (WR1, ">>Numbers.txt") or die"Could not open file";

for ($i=0; $i<= $#Read; $i++)
{
if($Read[$i] =~ /^Number:/ and $Read[$i+1] =~ /(\w\w\w\w\w\w-\d\d\d\d-\d\d\d\d)/)
{

$Number = $1;
$Number1 = substr ($Number, 7, 9);
print "$Number1\n";
print WR1"$Number1\n";
}
}
-------------------------------------------------------------------

The output of the script is Numbers.txt..

1999-1011
1999-0696
1999-0833
2000-1209

The problem with this is it is not useful to search pattern occurance more than once in the same line. The numers after 1999-0833 is not shown in thw output.
Any suggestions for this?

--Mahesh
Hi Mahesh,

First you have to post your code using the code tags and second you have not used
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
in your code.. Please follow this simple steps to ensure that people here can help you better..
Now regarding your question, you are not getting the numbers after 1999-0833 because you are considering only the first entry.. As above replied you should be using the split function.. Try this code

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. open (RD, ""pick no.txt") or die "Could not open file";
  6. my @Read = <RD>; close (RD);
  7.  
  8. open (WR1, ">>Numbers.txt") or die"Could not open file";
  9.  
  10. for (my $i=0; $i<= $#Read; $i++)
  11. {
  12.     if($Read[$i] =~ /^Number:/ and $Read[$i+1] =~ /(\w\w\w\w\w\w-\d\d\d\d-\d\d\d\d)/)
  13.     {
  14.     my @numbers_list = split(',',$Read[$i+1]);
  15. foreach(@numbers_list)
  16.     {
  17.          my $Number1 = substr ($_, 7, 9);
  18.         print "$Number1\n";
  19.         print WR1 "$Number1\n";
  20.     }
  21. }
  22. }
  23.  
And I noticed in your text file, some enteries have a space before the word "Number", and some dont.. If thats not typo, then you have to work on this code ...
Jan 29 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
I just posted a solution on perlguru, and heres the same question over here. :(
Jan 29 '08 #4

P: 3
I just posted a solution on perlguru, and heres the same question over here. :(
HI ALL
For searching a particular patter in a file u can use the grep function.

suppose u r searching for particular pattern like "PATTERN" in inputfile;

u can use
$returnvalue=`cat inputfile | grep "PATTERN"`
$returnvalue contain all the lines that are matched.Even u can redirect it also

`cat inputfile | grep "PATTERN > outputfile`

I welcome comments on the above solution
Jan 31 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
HI ALL
For searching a particular patter in a file u can use the grep function.

suppose u r searching for particular pattern like "PATTERN" in inputfile;

u can use
$returnvalue=`cat inputfile | grep "PATTERN"`
$returnvalue contain all the lines that are matched.Even u can redirect it also

`cat inputfile | grep "PATTERN > outputfile`

I welcome comments on the above solution
Yes you can. But calling shell scripts in a perl script is not a good use of perl . Plus it will not work on all operating system.
Jan 31 '08 #6

P: 3
Yes you can. But calling shell scripts in a perl script is not a good use of perl . Plus it will not work on all operating system.
thanks kelvin,
i tested grep i all unix flavours and it worked fine n i got results.
recently i started writing perl programmes
i would like to know the scope for perl programmers n in what concepts one has to master to take seriously perl programminng carrier.
it will be great help if i get answer for this.
Jan 31 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
thanks kelvin,
i tested grep i all unix flavours and it worked fine n i got results.
recently i started writing perl programmes
i would like to know the scope for perl programmers n in what concepts one has to master to take seriously perl programminng carrier.
it will be great help if i get answer for this.

Yes, grep should work in all Unix and Linux boxes, but not Windows, although it can be made to work with windows. Not sure about Macs.

But perl has it's own grep function and that can be used instead of shelling to the operating systems grep function.

To be a good perl programmer: First is to learn perl of course. But a good understanding of the popular operating systems is very helpful, as well as knowing how to use popular database applications like MySQL and others, learning regular expressions is a must, knowing the various communications protocols like TCP/IP, sockets, HTTP, FTP, are all helpful. CGI is a must if you plan on doing internet based applications, and I am sure there is plenty more.

I think being good at text parsing and data munging is the linchpin of a good perl programmer. Text processing is the root of perls beginnings and is fundamental to learning perl.
Jan 31 '08 #8

P: 3
Yes, grep should work in all Unix and Linux boxes, but not Windows, although it can be made to work with windows. Not sure about Macs.

But perl has it's own grep function and that can be used instead of shelling to the operating systems grep function.

To be a good perl programmer: First is to learn perl of course. But a good understanding of the popular operating systems is very helpful, as well as knowing how to use popular database applications like MySQL and others, learning regular expressions is a must, knowing the various communications protocols like TCP/IP, sockets, HTTP, FTP, are all helpful. CGI is a must if you plan on doing internet based applications, and I am sure there is plenty more.

I think being good at text parsing and data munging is the linchpin of a good perl programmer. Text processing is the root of perls beginnings and is fundamental to learning perl.

Thank you very much Kelvin
Jan 31 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.