473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to search a pattern occured more than once in line

4 New Member
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.tx t".

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 1797
mdshafi01
36 New Member
Even you can use split function to findout the number of occurance of that pattern in script.

shafi
Jan 29 '08 #2
mehj123
55 New Member
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.tx t".

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 Recognized Expert Specialist
I just posted a solution on perlguru, and heres the same question over here. :(
Jan 29 '08 #4
kiran312
3 New Member
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=`c at 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 Recognized Expert Specialist
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=`c at 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
kiran312
3 New Member
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 Recognized Expert Specialist
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
kiran312
3 New Member
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
3890
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 more than 10 letters, and find all possible mutations that match a word in my dictionary (80k words). However a wildcard letter '?' is also an acceptable character which increases the worst case time significantly. So if the letters are check...
17
6644
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. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
1
2144
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 vector to store this information as binary. Is there anyone who can help me please? I am a beginner in C++, and I have so far figured out how to read input from a user. I have a very long way to go, and I only have a week to do this. Any help...
32
14891
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
1
2723
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 Me.txtEmail.SetFocus Exit Sub End If Me.txtStatusBar.Value = "Parsing..." strEmail = Me.txtEmail.Value
14
2032
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
2947
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. Here's the script: # start of program # p.pl - fix broken SMTP headers in email files #
4
2124
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
1851
by: mercuryshipzz | last post by:
#!/usr/bin/perl #use strict; use warnings; sub search_pattern { my $file_name = $_;
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.