in reply to Re^2: Removing the carriage return in a Find & Replace?
in thread Removing the carriage return in a Find & Replace?

Because you've told perl to read the file a line at a time (well, more you haven't told it not to do otherwise and line is the default) so $_ will only contain <TD>\n and the next line will have <FONT ....>. At no point is the entire contents you expect to match in $_ simultaneously and in the right order so the match never happens and the substitution never triggers.

See the documentation for the -0 switch in perlrun, specifically the part about turning on paragraph mode.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^4: Removing the carriage return in a Find & Replace?
by mscharrer (Hermit) on Sep 26, 2008 at 13:51 UTC
    The -p option you use splits the input in separate lines. For Perl \n isn't the same as a space even if it is for HTML. One solution is to undef $/ in order to enable ''slurp mode'', (or to use the before mentioned command line option -0):

    perl -i -pe 'BEGIN { undef $/ } s/<TD>\s*<FONT\s+FACE=arial\s+SIZE=-1> +/widget/g' test.html
      How can I use this to batch edit multiple pages? For some reason, I can only edit one page at a time.

      Thanks!
      -Bob
Re^4: Removing the carriage return in a Find & Replace?
by bobafifi (Beadle) on Sep 26, 2008 at 15:54 UTC
    I just tried mscharrer variation on this and it worked!
    perl -i -pe 'BEGIN { undef $/ } s/<TD>\s*<FONT\s+FACE=arial\s+SIZE=-1>/widget/g' test.html

    Thanks so much!
    Bob