Zak
Zak

Reputation: 7525

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" {} \;

Upvotes: 0

Views: 266

Answers (1)

Andreas Louv
Andreas Louv

Reputation: 47117

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/ 

Upvotes: 2

Related Questions