简体   繁体   中英

sed ack search / replace line break with string

I have looked into a few SO threads, non of which have helped my specific situation.

I am trying to update a PHP app that I took over from php 5.6 to php 8.0

With that said there are MANY instances that look like:

<?
echo ...
function

I need to find all cases where <? is followed directly by a newline
and replace it with <?php(newline)

Per the SO posts I've read.. I think I am coming close with the following:

find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/\<\?\n/\<\?php\n/g" {} \;

I think I am close.. But I can't figure out why it won't replace <?\n with <?php\n as the sed statement works without the newline. But per THIS POST it looks like I am doing it correctly.

What am I doing wrong?

Iterations I've tried:

$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/\<\?\n/\<\?php\n/g" {} \;

$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<\?\n/<\?php\n/g" {} \;

$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\n/<?php\n/g" {} \;

$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\n\r/<?php\n/g" {} \;

$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\r\n/<?php\n/g" {} \;

The sed command itself could be something as simple as:

sed -i 's/<?$/<?php/'

Glue that together with find and it might work for you.

$ is an anchor matching the end of a line, you might consider using ^ to anchor the match to the beginning as well:

s/^<?$/<?php/ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM