in reply to find and replace from shell

Using the -p switch means that you are only reading one line at a time.    You need to either read in the whole file or change the Input Record Separator to something like "}\n".

This may work, but it is UNTESTED:

perl -i -pe' BEGIN { $/ = "}\n" } s/@{[ <<REGEX ]}/@{[ <<STRING ]}/g; if ((!(\$session_login)) or (\$session_access < 5)) { header("Window-Target: _top"); header("Location: index.php"); } REGEX if ((!(\$session_login)) or (\$session_access < 5)) { header("Window-Target: _top"); header("Location: index.php"); exit; } STRING ' *.php

Replies are listed 'Best First'.
Re^2: find and replace from shell
by johnkasta (Initiate) on Jun 16, 2010 at 19:58 UTC
    jwkrahn, thanks for your input!

    1. please excuse the noobie question but when i copy and past this into my shell, iterm lays it out vertically and does not wait for my 'enter' to execute. how do i execute this in the shell?

    2. perhaps it would be easier to change the -p to something else to tell it to read the whole file. what should I replace the -p with?
    Thanks!

      1. In my shell, (xterm) when I want to insert a newline I enter the two keys <CTRL>vj.
      2. To read the whole file you need to use the -0 switch with the value -0777 along with the -p switch.
        Thanks for your replies,

        when executing this in iterm..
        perl -i -pe' BEGIN { $/ = "}\n" } s/@{[ <<REGEX ]}/@{[ <<STRING ]}/g; if ((!(\$session_login)) or (\$session_access < 5)) { header("Window-Target: _top"); header("Location: index.php"); } REGEX if ((!(\$session_login)) or (\$session_access < 5)) { header("Window-Target: _top"); header("Location: index.php"); exit; } STRING ' *.php

        I get back the following error messages
        Can't find string terminator "REGEX" anywhere before EOF at -e line 3.
        -bash: BEGIN: command not found
        -bash: s/@{[: No such file or directory
        I am probly missing something obvious, due to no EOF usage experience.
        Any help on these err messages is greatly appreciated!
Re^2: find and replace from shell
by johnkasta (Initiate) on Jun 16, 2010 at 20:41 UTC
    So the the BEGIN { $/ = "}\n" } changes the input record separator?

    Should I substitute REGEX with the regex for the find, and the STRING with the replace? Not sure how much of this actually goes in the shell prompt. Did you write REGEX and STRING as placeholders? Can anyone assist?

      Not sure if you have been answered in the Chatterbox already, so...

      So the the BEGIN { $/ = "}\n" } changes the input record separator?

      Yes. See $/ in perlvar.

      Should I substitute REGEX with the regex for the find, and the STRING with the replace? Not sure how much of this actually goes in the shell prompt. Did you write REGEX and STRING as placeholders? Can anyone assist?

      I, also, cannot test jwkrahn's code of Re: find and replace from shell, but it looks like it should work as it stands. The  <<REGEX and  <<STRING begin 'here-documents': see <<EOF in perlop. What happens when you try the code on an expendable test file?