in reply to Need RegEx assistance

Here's a one-liner - I'm sure it could be golfed...

perl -ibak -a -ln00e "$,=qq/\n/;($t)=$F[1]=~'/([^/]*)$';$F[0]=~s/\[/[$t /;print @F,qq/\n/" filename

Here's another way - I moved your data into __DATA__ to simplify.

#!C:\Perl\bin\perl -w use strict; #paragraph mode - see perlvar, input record separator $/ = ""; while(<DATA>) { my @lines = split /\n/, $_; my $file; $lines[0] =~ s/\[/\[$file / if ($file) = $lines[1] =~ '/([^/]*)$'; print join ("\n", @lines), "\n\n"; } __DATA__ [get4] $uri=/LRA/RiskApplicationPage.aspx $method=GET [get5] $uri=/LRA/CSS/RiskManagement.css $method=GET

The key is to change the input record separator, so each time you read you get a single complete record to manipulate.

Update: added "'" (single quote) in re line 10

Replies are listed 'Best First'.
Re: Re: Need RegEx assistance
by Anonymous Monk on Jul 12, 2002 at 21:43 UTC
    Thank you very much, but I'm having a rough time executing the code, I get the following error:
    Unmatched [ before HERE mark in regex m/<[ << HERE ^ at line 10
      Unmatched [ before HERE mark in regex m/<[ << HERE ^ at line 10
      Please cut and paste or 'd/l code', don't try to retype it. If you don't see the 'd/l code' link, reparent by clicking on the node title. You will see it.
      #m/<[ # ^ should be '/' m/\[ # ^
        This is the code compliments of jsprat
        #!C:\Perl\bin\perl -w use strict; #paragraph mode - see perlvar, input record separator $/ = ""; while(<DATA>) { my @lines = split /\n/, $_; my $file; $lines[0] =~ s/\[/\[$file / if ($file) = $lines[1] =~ /([^/]*)$'; print join ("\n", @lines), "\n\n"; } __DATA__ [get4] $uri=/LRA/RiskApplicationPage.aspx $method=GET [get5] $uri=/LRA/CSS/RiskManagement.css $method=GET
        I receive the following error:
        Unmatched [ before HERE mark in regex m/([ << HERE ^/ at C:\Perl\greg2 +.pl line 10.
        I'm using the following code compliments of jsprat:
        #!C:\Perl\bin\perl -w use strict; #paragraph mode - see perlvar, input record separator $/ = ""; while(<DATA>) { my @lines = split /\n/, $_; my $file; $lines[0] =~ s/\[/\[$file / if ($file) = $lines[1] =~ /([^/]*)$'; print join ("\n", @lines), "\n\n"; } __DATA__ [get4] $uri=/LRA/RiskApplicationPage.aspx $method=GET [get5] $uri=/LRA/CSS/RiskManagement.css $method=GET
        and I receive the following error:
        Unmatched ( before HERE mark in regex m/( << HERE \[^/ at C:\Perl\Test +.pl line 10.
      I somehow deleted a single quote in line 10 when I posted it - take a look at the update. Not a very clear error message, huh?

      BTW, I'd have /msg'ed you, but you aren't logged in ;-) Maybe you should create an account?

        Thanks jsprat, you were a great help, I always learn something new, Thanks again!