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

Dear monks, i seek for your wisdom...

i want to extract the whole sentence (line)and keep it to a string only if a search word is found in this sentence. also is it possible, in the result the word searched to be displayed in bold?

the text file that has to be searched looks like this:

number:.........#end of line ......................................................#end of line + number:........#end of line .....................................................#end of line number:.......#end of line ......................................................#end of line
thanks for your advice

Replies are listed 'Best First'.
Re: extract whole sentence after a match of a word
by hipowls (Curate) on Feb 26, 2008 at 12:17 UTC

    Not quite sure what you are asking since your data isn't formatted, put <code> and </code> tags around the data and it will appear as you expect.

    I'll guess that you want to collect whole lines that have a certain word

    my @lines; while ( my $line = <$input_fh> ) { push @lines, $line if $line =~ /WORD/; }
    If this wasn't your question then, if you reformat your data and explain where I was wrong, we will be better able to help.

Re: extract whole sentence after a match of a word
by olus (Curate) on Feb 26, 2008 at 12:36 UTC
    use strict; use warnings; my @all_lines = <DATA>; # get the lines that match the word 'number' my @lines = grep /number/, @all_lines; print "Lines that matched\n"; print for @lines; # get and highlight the lines that match the word 'number' my @bold = map { $_ =~ /number/ and s/number/<b>number<\/b>/g ? $_ : ( +); } @all_lines; print "\nHighlighted word\n"; print for @bold; __DATA__ number.................... ..................... ......number.............. ......................... ..................number

    outputs:

    Lines that matched number.................... ......number.............. ..................number Highlighted word <b>number</b>.................... ......<b>number</b>.............. ..................<b>number</b>
Re: extract whole sentence after a match of a word
by moritz (Cardinal) on Feb 26, 2008 at 12:20 UTC
    It's hard to guess what you are up to, but usually that kind of problem can be solved by first spliting the text into records (here: sentences), and then searching in each of the records for the word you are looking for.

    also is it possible, in the result the word searched to be displayed in bold

    That depends on how the result is displayed. If the output goes into a terminal, you can use Term::ANSIColor for the formatting, other choices of output require other things to do.

    As you can see your questions are rather vague, so the answers must be as well.

Re: extract whole sentence after a match of a word
by Erez (Priest) on Feb 26, 2008 at 12:42 UTC

    When you search for a regex, you can match it against several elements in the string. Assuming you mean a literal number, rather than the word "number", use m/^(\d+)(.+)$/, as it will only match if there's a number at the very start.

    Once matched, the number itself will be assigned to $1, and the rest of the line, up to the EOL mark, to $2.
    Make sure to match only what you need, though. If the number is displayed as 'xxxx.xx', or 'xx,xxx', the regex will fail.
    As for the second part, yes, it is possible. How is a question of the output format, so more information is needed.

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphores.

Re: extract whole sentence after a match of a word
by alkis (Acolyte) on Feb 27, 2008 at 15:35 UTC
    thanks for the advice, sorry for being so vague (i am a newbie), i will examine your answers and i will be more precise to describe you my problem... thanks again