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
8 1769
Even you can use split function to findout the number of occurance of that pattern in script.
shafi
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 - use strict;
-
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 - #!/usr/bin/perl
-
use strict;
-
use warnings;
-
-
open (RD, ""pick no.txt") or die "Could not open file";
-
my @Read = <RD>; close (RD);
-
-
open (WR1, ">>Numbers.txt") or die"Could not open file";
-
-
for (my $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)/)
-
{
-
my @numbers_list = split(',',$Read[$i+1]);
-
foreach(@numbers_list)
-
{
-
my $Number1 = substr ($_, 7, 9);
-
print "$Number1\n";
-
print WR1 "$Number1\n";
-
}
-
}
-
}
-
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 ...
I just posted a solution on perlguru, and heres the same question over here. :(
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
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.
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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...
|
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...
|
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
|
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.
...
|
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:
|
by: mercuryshipzz |
last post by:
#!/usr/bin/perl
#use strict;
use warnings;
sub search_pattern
{
my $file_name = $_;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |