in reply to Selecting text between given words (multi-lines (/n) and multiple occurrences)

Sounds like a good use for the .. operator in scalar context.
open FH, "file.txt" or die "Couldn't open file.txt: $!"; while (<FH>) { if (/^begin:$/ .. /^end:$/) { print; # or do whatever else you want here. } }

Replies are listed 'Best First'.
Re: Re: Selecting text between given words (multi-lines (/n) and multiple occurrences)
by nylon (Acolyte) on Aug 26, 2003 at 08:59 UTC
    First things first: Paladin thx :-)

    How do I get the actual data between begin & end in an output file? When I try, the file stays empty.
    #----------------------------------------------------------- #!/usr/bin/perl (This code does not work !!!) # # between.pl <output file><input file><begin word><end word> # open (FH_output, ">> $ARGV[0]") or die "Couldn't open file.txt: $!"; open FH_input, "< $ARGV[1]" or die "Couldn't open file.txt: $!"; while (<FH_input>) { if (/^$ARGV[2]$/ .. /^$ARGV[3]$/) { $info = $_ ; print FH_output $info; } } close (FH_input) ; close (FH_output) ;
    Thanks,

    Firewall (Perl is fun. I'm going to study it some more. But still a long way to go :-)