473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to search a pattern occured more than once in line

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
8 1769
Even you can use split function to findout the number of occurance of that pattern in script.

shafi
Jan 29 '08 #2
mehj123
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
4,059 Expert 2GB
I just posted a solution on perlguru, and heres the same question over here. :(
Jan 29 '08 #4
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
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

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

Similar topics

10
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
1
by: kittykat | last post by:
Hi, I want to read the input variables from a user, and then compare this with data in the text file. If the input variables and the data in the text file match, then i would like to create a...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
14
by: henrik.sorensen | last post by:
Hi List, I am looking for a way to do a case insensitive search for file names. Anybody have some hints ? thanks Henrik pl1gcc.sourceforge.net
10
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. ...
4
by: mosesdinakaran | last post by:
Can any one explain how the rule is applied for the following Regular expression $Str = 'the red king'; $Pattern = '/((red|white) (king|queen))/'; preg_match($Pattern,$Str,$Val); Result:
3
by: mercuryshipzz | last post by:
#!/usr/bin/perl #use strict; use warnings; sub search_pattern { my $file_name = $_;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.