Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to do the following:
1. open a file to search for some specific text.
2. take a 'specific range of text' from the line where my search was found.
3. pass the text in the 'specific range' to a variable so i can print it out.
example:
I have the following text in a file. Everything in the line below is in the file along with lots of other text.

<Directory Filename="the_specific_range_of_text_is_in_here" Version="1.9">

1. Now I want to be able to search for the first part which is always constant:
<Directory Filename=
2. Then I want to take everything after the search in #1 above, but only the stuff in between the quotations:
"the_specific_range_of_text_is_in_here"
3. But ignore the text from the rest of the line:
Version="1.9">

Whatever text was in between the quotations in #2 above, I want passed to a variable so I can use this for printing to some output.

I've seen many search and replace scripts, but I can't seem to find anything that does exactly what I want.

Please help.

20040729 Edit by ysth: change /br tags to br/

  • Comment on search file for text then only use some of the text in the line

Replies are listed 'Best First'.
Re: search file for text then only use some of the text in the line
by Joost (Canon) on Jul 29, 2004 at 19:40 UTC
      Thank you VERY MUCH!
      I've been trying to get this working for days now and actually got it working but I had used about 20 lines of code to do what you did in 5.
      Your solution is perfect. THANK YOU!
Re: search file for text then only use some of the text in the line
by murugu (Curate) on Jul 30, 2004 at 05:16 UTC

    Try this,

    undef $/; $k=<DATA>; while ($k=~/<Directory Filename="([^"]+)"/g) { $value=$1; print $value; } __DATA__ <Directory Filename="hai" Version="1.9"> <Directory Filename="dai" Version="1.9">