LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   sed script to append variable text (https://www.linuxquestions.org/questions/linux-general-1/sed-script-to-append-variable-text-514064/)

gmartin 12-27-2006 11:41 AM

sed script to append variable text
 
I have a php source code file and I want to add a line immediately following each line containing a function declaration. That's easy enough using sed and the /a\ command. But, that's not enough for me. I want to include in the appended text the name of the function. For the purpose of this exercise, I'll be satisfied with everything that is not 'function'

For example:
Code:

function UserLib($db) {

  some other code;
 }

would become:
Code:


 function UserLib($db) {
 debug("UserLib($db) {" )
  some other code;
 }

Questions is how do I get what remains after the match?

I tried this two-liner to match regions and use the resulting tokens, but they don't get substituted correctly in the output.
Code:

/\(function\) \(.*\)/a\
debug(\2)

and
/\(function\) \(.*\)/a\
debug("\2")

and
/\(function\) \(.*\)/a\
debug('\2')

I know I'm not the first one down this path. Is sed the correct tool?

Thanks

colucix 12-27-2006 01:32 PM

I think this is better achieved with a simple Gawk script

Code:

{ print $0 }

/function/ {

    printf "debug(\"%s {\" )\n",$2

}

but you have to be sure that the regexp "function" does not appear in other parts of the code to process! Bye

theNbomr 12-27-2006 01:37 PM

Sed may be the correct tool for many, but for my taste, anything that complex is too much for sed. My weapon of choice: perl. In perl, you can more easily generalize the problem to handle any valid php syntax, and not rely on your own conventions of style and formatting. In my view, the procedural nature of perl makes the problem easier to generalize. Not being a sed expert, I would find it difficult to manage things like variable numbers of PHP function arguments, and function declarations split across multiple lines, to name a couple of obstacles.

If no one else contributes a solution, I will work on a perl version and post it when it is ready. Your idea looks useful enough that I can probably use it on other language source codes as well.

--- rod.

gmartin 12-27-2006 01:59 PM

I'm no gawker, but I'll give it a try - thanks

\\Greg


Quote:

Originally Posted by colucix
I think this is better achieved with a simple Gawk script

Code:

{ print $0 }

/function/ {

    printf "debug(\"%s {\" )\n",$2

}

but you have to be sure that the regexp "function" does not appear in other parts of the code to process! Bye


gmartin 12-27-2006 04:44 PM

@colucix
The gawk approach worked. The only problem I had was a few line where the function's opening brace "[" was not on the same line as "function". I manually fixed those lines. I guess using a greedier match could fix that, I might lose the ability to put the function name in the debug call. Also the side effect of grabbing the function name and the paramters winds up printing the parameter values as well.

All in all - a good solution.

Thanks!


All times are GMT -5. The time now is 08:08 AM.