Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: get n lines before or after a pattern

by kennethk (Abbot)
on Jul 25, 2012 at 15:16 UTC ( #983679=note: print w/replies, xml ) Need Help??

in reply to get n lines before or after a pattern

What have you tried? What didn't work? See How do I post a question effectively?.

There are two ways I can think of doing this. Probably the simpler from your perspective would be to iterate over lines in a while loop, and set up some state variables to stash values. Then, when you hit a lastname line, you can test the value of $name (or $hash{name}) to see if it is jack, outputting all relevant information if it is.

The more complex approach would be using regular expressions with the m and g modifiers. This is how I'd do, but tends to be a little more fragile, less obvious for code review and more challenging for the neophyte.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983679]
[Corion]: ... surrounding HTML to scrape all the other notes. Maybe I should better look at dumping all the requests that pass between Google and my "browser" instead.
[choroba]: The older one will even perform twice, once at a retirement home, and then at the music school. It's a day off, but will be pretty busy...
[Eily]: hello perlmonks :)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-12-12 08:59 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (327 votes). Check out past polls.