Right...but my problem is I don't want the substitutions to happen on ALL the 
lines matched. Notice from the below input and output examples I do a grep to 
search for "0 AAA ".
I then do a substitution on "0 AAA " to "0 AAA BBB ". Then the first occurence 
of "XXX" after the "0 AAA " substitution gets replaced with "111 XXX" and ALL 
other "XXX" gets ignored (no 
substution on these) until the next "0 AAA " and so on...

I have also included by script below. The below script substitues the first 
match by using ?XXX? in the GREP but not the next "XXX" following next grep 
match on "0 AAA "

I'm starting to babble...


Here is the input file:

0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
You want to find what. You want to find what? 0 0 0 AAA 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
You want to find what. You want to find what? 0 0 0 AAA 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 

Here is what the output file should look like:

0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
You want to find what. You want to find what? 0 0 0 AAA BBB 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
111 XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
You want to find what. You want to find what? 0 0 0 AAA BBB 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
111 XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
0101 0101 0101 0101 0101 0101 0101 0101 0101 
XXX 


test.pl
=====


#!/usr/local/bin/perl
#use strict;
my $ifile = "file.in";
my $ofile = "file.out";
my $line;

open (INFILE, "< $ifile");
        open (OUTFILE, "> $ofile");
                foreach $line (<INFILE>) {
                        if (grep(/0 AAA /, $line)) {
                                $line =~ s/0 AAA/0 AAA BBB/g;
                        }
                        if (grep(?XXX?, $line)) {
                                $line =~ s/XXX /111 XXX /g;
                        }
                print OUTFILE $line;
                }
        close(OUTFILE);
close(INFILE);

#rename($ofile, $ifile);


On Wed, 24 Nov 2004 12:29:13 -0500 (EST), Chris Devers wrote:

>On Wed, 24 Nov 2004, FlashMX wrote:
>
>> I'm trying to understand the logic. When you open a file each line is 
>> read in one at a time. At that point you can do whatever you want to 
>> that line. In my case a search and replace.
>>
>> Can I do two search and replaces on the same line or would I have to 
>> open the file again for a second pass?
>
>You answered your own question -- "you can do whatever you want to that 
>line". 
>
>    while <> {
>        my $line = $_;
>        $line = sub_one( $line );
>        $line = sub_two( $line );        
>    }
>
>Etc.
>
>
>
>-- 
>Chris Devers




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to