| Author |
perl pattern matching
|
anandkumar biyani
Greenhorn
Joined: Dec 06, 2011
Posts: 9
|
|
Hello everyone,
Can anyone help me out how to extract the string after matching the pattern.
Ex:I have a file containing line as below
Number of rows read = 730
I want to extract the value 730.
Thanks in advance.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 8428
|
|
your specs are too vague.
do you want what is after the '='? do you want what is after "Number of rows read = " (and do the spaces after the '=' make a difference?) do you want the numeric part? What if the file contains the line "Number of files that contain 5 elements = 730"?
Giving one example of what you want to do does NOT make a spec. And how you would write this would entirely depend on what, EXACTLY, you want to do. So give us the details and we can help you.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
anandkumar biyani
Greenhorn
Joined: Dec 06, 2011
Posts: 9
|
|
Hiii,
Actually i want to match for the pattern Number of rows read and extract the value 730.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 2688
|
|
You need a pattern that matches against the whole expression, and that contains a capturing group - a bit in brackets - that is the bit you want to extract. Then, once you've matched it, you can use the variables $0, $1, $2 etc. $0 is the whole string matches, $1 is the first sub-group, etc.
A simple example: if I know a string is a date in the format 2011-07-19, then I can extract the parts like this:
(N.B. not checked - sorry if I got the syntax wrong somewhere).
So that's the mechanism - you just need the right expression. Exactly what that is depends on the exact rules that apply in your situation.
|
 |
anandkumar biyani
Greenhorn
Joined: Dec 06, 2011
Posts: 9
|
|
i have a file which read like this
Number of rows read = 730
Number of rows inserted = 730
Number of rows rejected = 730
Number of rows comitted =730
I want to verify if number of rows read and committed are same. Hence i need to extract those values and compare.
Hence i want to extract those numbers and compare.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 8428
|
|
So you have the string and you need to extract the numeric part?
|
 |
 |
|
|
subject: perl pattern matching
|
|
|