0

I have PHP scripts that have code that I want to change from:

Function1_show();Function2_show();

And I want the file to be changed to this:

call_my_func("Function1");
call_my_func("Function2");

I don't use regex very often so I am not sure how to key on the "_show();" on the end of each function. SED seems like the best function for this but any help is appreciated.

2
  • What are the rules for the names Function1, Function2? are they always composed of alphanumeric characters only? Aug 11 '19 at 12:47
  • What are the names of those functions, do they always start with "Function_"? Is there always an underscore? Do they all end in "_show()"?
    – Ned64
    Aug 11 '19 at 16:36
0
sed -i 's/\([A-Za-z0-9_]*\)_show();/call_my_func("\1");/g' *.php

The -i option sell sed to update the *.php files in-place.

This searches for alphanumeric text (including underscores) followed by _show(); and captures that text.

This whole string gets replaced by call_my_func(", the captured text, and ");.

This gets repeated if the match text occurs more than once because of the modifier g.

It would have been possible to use [a-z0-9_] without A-Z by using the i modifier, but then it would also match Function_SHOW(); which is probably not what is desired.

1
  • That is perfect. Thanks for the help!
    – RRainey
    Aug 12 '19 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.