Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

perl pattern matching

 
anandkumar biyani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12090
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
anandkumar biyani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiii,

Actually i want to match for the pattern Number of rows read and extract the value 730.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12090
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have the string and you need to extract the numeric part?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic