Tell me how to find a piece of code, in my case like this:

<div class="site-info"> </div><!-- .site-info --> 

in any php files in all subfolders of the current folder and replace it with another piece of code. The nesting level of folders is not limited, that is, in all php files of all folders and subfolders of the current folder.

1 answer 1

Alas, using the features of bash is very difficult to implement.

but you can use other programs from the operating system. for example, the find (for search) and sed (for changing the contents of the file) programs will greatly help.

in any php files in all subfolders of the current folder

 $ find -type f -name \*.php 

replace it with another piece of code

as I understand it, the initial and final lines are given, and everything between these lines (including these lines themselves) must be replaced with some text:

 $ sed -i '/начальная строка/,/конечная строка/c\какой-то текст' файл 

it remains only to “join” these two programs together so that the second receives a list of files from the first. for this you can use, for example, the xargs program:

 $ find опции.и.параметры | xargs sed опции.и.параметры 

or, for your particular case:

 $ find -type f -name \*.php | xargs sed -i '/<div class="site-info">/,/<\/div><!-- .site-info -->/c\какой-то текст' 

additional reading:

 $ man find $ man sed $ man xargs 
  • @alexader No, this is not the starting and ending lines, this is the piece that needs to be replaced, - Novice
  • I need to insert the code between these two lines, but there are files where the code is already inserted between them, hence the option with starting and ending lines will not work - Novice
  • Ie, three specific lines? then the program for sed will have to complicate things a little. For the future, I recommend that you formulate the question more clearly and remove everything that is irrelevant. such as “how to find files” or the requirement to use a shell program to perform actions for which it is absolutely not intended. - aleksandr barakin pm
  • Stand do not change - yes, I was not going to change anything. the complication of the program you need, in view of the uselessness of anyone besides you, would be logical to leave to you as homework. - aleksandr barakin
  • one
    because you are not talking about line fragments, but about lines. - aleksandr barakin