LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-27-2006, 11:41 AM   #1
gmartin
Member
 
Registered: Mar 2003
Location: PA
Distribution: Slackware 13.37 Linux Reg # 341245
Posts: 285

Rep: Reputation: 40
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
 
Old 12-27-2006, 01:32 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 12-27-2006, 01:37 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 12-27-2006, 01:59 PM   #4
gmartin
Member
 
Registered: Mar 2003
Location: PA
Distribution: Slackware 13.37 Linux Reg # 341245
Posts: 285

Original Poster
Rep: Reputation: 40
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
 
Old 12-27-2006, 04:44 PM   #5
gmartin
Member
 
Registered: Mar 2003
Location: PA
Distribution: Slackware 13.37 Linux Reg # 341245
Posts: 285

Original Poster
Rep: Reputation: 40
@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!
 
  


Reply

Tags
sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 07:07 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration