[CLUE-Tech] Extracting whole function blocks from multiple files for comparison

Keith Hellman kehellman at yahoo.com
Tue Oct 29 17:13:31 MST 2002


The following works:
<snip>
js$ cat test.js

/* comment */

function deadbeef() {
	Response.write("Mad Cow");
}

function foo() {
	var x=5;
	var y=10;
	var bar="Joe's";
	for(x=0;x<=y;x++) {
		Response.write("<br>CLUE-TECH RULES");
		if(x == 6) {                          
			Response.write("Eat at " + bar);
		}                                   
	}
}

function bar() {
	return
}

js$ cat test.js |sed -n '/^function[    ]foo/,/^}/p'
function foo() {
	var x=5;
	var y=10;
	var bar="Joe's";
	for(x=0;x<=y;x++) {
		Response.write("<br>CLUE-TECH RULES");
		if(x == 6) {                          
			Response.write("Eat at " + bar);
		}                                   
	}
}
</snip>

You could wrap the sed call in a shell script that accepts the 
input file and function name and saves the output to func-inputname.js.
The sed script is dependent of course on well formatted js code.  If
this is not the case, you'd probably want to use python/perl to catch
the function declaration by regular expression, then count {} till it
reaches the end of the function (ignoring, of course, braces in stings
:^).  This would depend on syntatically correct js - of course if your
diffing the output, it would seem you've made this assumption already.

You mentioned that you usually do this with vi; just wanted to point out
that vim can run a script from a command line parameter.  This would be
a plausible solution as well (whatever gets the job done ya know...)

BTW: the [  ] in the sed expression is the old fashioned way of saying
whitespace (space + tab).  The RE isn't perfect but it is a start.

HTH
-- 
Keith Hellman                             #include <disclaimer.h>
kehellman at yahoo.com               from disclaimer import standard




More information about the clue-tech mailing list