Search between two words in a file
Anonymous Monk
created: 2004-06-14 06:00:09
How can I search between two words in a file using perl e.g. a file contains some paragraphs and in each paragraph there are some words between "FROM" and "WHERE". My query is how to search between these two words and store the data.

Edit by BazB: ucfirst title.

Re: Search between two words in a file
created: 2004-06-14 06:32:25
One way would be to read each paragraph into a single variable (either by setting $/ or by appending line to the variable until you get to the end of the paragraph), then simply match using something like $paragraph=~/FROM(something wild)WHERE/;
Re^2: Search between two words in a file
created: 2004-06-14 07:49:06
Can u just send me the script as i am totally new to perl scripting and i have been assigned this task to do.
Re^3: Search between two words in a file
created: 2004-06-14 10:41:06
Can "u" just send us your next paycheck?

--
[ e d @ h a l l e y . c c ]

Re^2: Search between two words in a file
created: 2004-06-14 07:58:55

To the Original Poster:
The manuals   perldoc perlrequick   and   perldoc perlre   describe it quite well.

You will understand matija's solution after a quick glance into one of them and be able to use it.

$paragraph =~ m/FROM(.*?)WHERE/; # now everything in between is in   $1
my $inbetween = $1;

If that still doesn't make much sense, everything else can be found in   perldoc perlintro.

Cheers, Sören

Re^2: Search between two words in a file
created: 2004-06-15 01:51:31
can you give me the command used for appending line to a variable until end of line. and after matching the data using "$paragraph=~/FROM(something wild)WHERE/;", how can I print that variable?
Re: Search between two words in a file
created: 2004-06-14 06:53:16

1) What have you tried?

2) Have you considered the possibility of embedded start/end strings? (eg 'rabbit FROM foo FROM bar WHERE baz WHERE')

perlmonks.org content © perlmonks.org and Anonymous Monk, halley, Happy-the-monk, matija, Not_a_Number

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03