column: # make column f='vgs_n "Id(Mn)"'
	-@cat |awk '\
function same(a,b,i){for(m=i=0;i<=length(a);i++)if(substr(a,i,1)==substr(b,i,1)){m++}{return (m==i)}} \
BEGIN{ \
	for(i=2;i<ARGC;i++){c[++c[0]]=ARGV[i]}ARGC=1 \
}/^#/{ \
	next \
}/^ /{ \
	for(i=1;i<=c[0];i++) \
	{ \
		for(j=1;j<=NF;j++) { \
			if(same($$j,c[i])) \
			{ \
				f[i]=j; \
				printf" %s%c",$$j,i==c[0]?"\n":"\t" \
			} \
		} \
	} \
}/^[^ #]/{ \
	for(i=1;i<=c[0];i++){printf"%s%c",$$f[i],i==c[0]?"\n":"\t"} \
}' - $f

list: # make list f='vgs_n "Id(Mn)"'
	-@cat |awk -v OFS=, '\
function same(a,b,i){for(m=i=0;i<=length(a);i++)if(substr(a,i,1)==substr(b,i,1)){m++}{return (m==i)}} \
BEGIN{ \
	for(i=2;i<ARGC;i++){c[++c[0]]=ARGV[i]}ARGC=1 \
}/^#/{ \
	next \
}/^ /{ \
	for(i=1;i<=c[0];i++) \
	{ \
		for(j=1;j<=NF;j++) { \
			if(same($$j,c[i])) \
			{ \
				printf"%d%c",j,i==c[0]?"\n":OFS \
			} \
		} \
	} \
}/^[^ #]/{ \
	exit \
}' - $f

plot: #make plot
	-@(echo 'plot "-" with lines';cat;echo e) |gnuplot -persist

ascii:
	-@awk ' \
/^#/{ \
	next ; \
}/^ /{ \
	printf"Flags: real forward %s\n",(NF>2)?"stepped":""; \
	param[0] = NF ; \
	for(i=1;i<=NF;i++){param[i]=$$i} \
}/^[^ ]/{ \
	l[record++] = $$0 ; \
}BEGIN{ \
	print "Title: * '$${HOSTNAME}' '$${PWD}'" ; \
	"LANG= date"|getline; \
	print "Date: "$$0 ; \
	print "Plotname: Generated from a .rdb file" ; \
}END{ \
	print "No. Variables:	"param[0] ; \
	print "No. Points:	"record ; \
	print "Offset: 0" ; \
	print "Variables:" ; \
	for(i=0;i<param[0];i++) \
	{ \
		$$0=param[i+1] ; \
		printf"\t%d	%s	%s\n",i,$$0,\
		(/^[vV]/)?"voltage":(/^[iI]/)?"device_current":"parameter" ; \
	} \
	print "Values:" ; \
	if (param[0]>2) { \
		for(j=0;j<record;j++) \
		{ \
			printf"%d\t",j ; \
			$$0=l[j] ; \
			for(i=1;i<=NF;i++) \
			{ \
				print "\t"$$i ; \
			} \
		} \
	} \
	else \
	{ \
		for(i=0;i<record;i++) \
		{ \
			print "\t"l[i] ; \
		} \
	} \
}'

sx:
	-@cat |awk -v OFS='\t' ' \
/^#/{ \
	next ; \
}/^ /{ \
	gsub(/\r/,"") ; \
	abscissa=$$1 ; \
	ordinate=$$2 ; \
	printf" %s\t %s\t d_%s\t dd_%s\n",abscissa,ordinate,ordinate,oridinate >"/dev/stderr" ; \
	pline=$$0 ; \
}{ \
	ZDY=zdy+1e-15 ; \
	if($$0!=pline) \
	{ \
		gsub(/\r/,"") ; \
		dx = $$1 - zx ; \
		zx = $$1 ; \
		dy = $$2 - zy ; \
		zy = $$2 ; \
		ddy = dy - zdy ; \
		zdy = dy ; \
		printf"%g\t%g\t%g\t%g\n",$$1,$$2,dy,ddy >"/dev/stderr" ; \
		if (record > 2 && ddy * zddy <= 0) \
		{ \
			last_slope=dx/ZDY ; \
			print "#slope"last_slope >"/dev/stderr" ; \
			last_intercept=zx-dx*zy/ZDY ; \
			print"#xintercept"last_intercept >"/dev/stderr" ; \
			found++ ; \
		} \
		zddy=ddy ; \
		record++ ; \
	} \
	pline=$$0 ; \
}END{ \
	if (record > 0) \
	{ \
		print " slope"," xintercept" ;\
		print found?last_slope:dx/ZDY, \
			found?last_intercept:zx-dx*zy/ZDY ; \
		exit 0 \
	} \
	exit 1 \
}' 2>/dev/null

dat: #gnuplot .dat file # make dat p=4
	-@cat |awk -v OFS='\t' -v p=$$p -F'\t' ' \
$$p!=l[p]{ \
	print"" ; \
} { \
	print ; \
	split($$0,l) ; \
}'
	
d:
	-@cat |awk -v OFS='\t' -F'\t' ' \
/^#/{ \
	gsub(/\r/,"") ; \
	gsub("#","",$$NF) ;\
	$$NF="#"$$NF"\t#d"$$NF ; \
	print$$0 ; \
	pline=$$0 ; \
}{ \
	if($$0!=pline) \
	{ \
		gsub(/\r/,"") ; \
		print$$0,$$NF-last ; \
		last = $$NF ; \
	} \
	pline=$$0 ; \
}' 2>/dev/null

sxp: #make sxp x=4  y=6 [ #p1 #p2 ... #pn  #x #y1 #y2 #y ... #dy #ddy ]
	-@cat |awk -v OFS='\t' -v x=$$x -v y=$$y -F'\t' ' \
/^#/{ \
	print $$0,"#slope","#xintercept" ; \
	pline=$$0 ; \
}{ \
	if($$0!=pline) \
	{ \
		dx = $$x - zx ; \
		zx = $$x ; \
		dy = $$(NF-1) ; \
		zy = $$y ; \
		ddy = $$NF ; \
		zdy = $$(NF-1) ; \
		if (record > 2 && ddy * zddy <= 0) \
		{ \
			ZDY=zdy+1e-15 ; \
			last_slope=dx/ZDY ; \
			last_intercept=zx-dx*zy/ZDY ; \
			print $$0,last_slope,last_intercept ; \
		} \
		zddy = $$NF ; \
		record ++ ; \
	} \
	pline=$$0 ; \
}'

last:
	-@ awk '$1!=p[1]||$2!=p[2]||$3!=p[3]{print pline}{split($0,p);pline=$0}'

ltspice: # <in make ltspice >out
	-@cat|awk ' \
NR==1{ \
		for(i=1;i<=NF;i++){printf" "$$i;if(i!=NF){printf"\t"}}\
}/^Step/ && !headered++ { \
		for(f=1;f<=NF;f++) if($$f~/=/) \
		{ \
			param[0]++ ;\
			label=$$f ;\
			gsub("=.*$$","",label) ; \
			printf"\t "label ; \
		} \
		printf"\n" ;\
}/^Step/{ \
		p=0 ;\
		for(f=1;f<=NF;f++) if($$f~/=/) \
		{ \
			gsub("^.*=","",$$f);gsub("n$$","e-9",$$f); \
			gsub("u$$","e-6",$$f);gsub("m$$","e-3",$$f); \
			param[++p]=$$f ;\
		} \
}/^\t/{ \
	if(!headered++) {printf"\n"}\
	sub("^\t","");printf$$0 ;\
	if (param[0]) for(p=1;p<=param[0];p++) \
	{ \
		printf"\t%s",param[p] \
	} \
	printf"\n" ;\
}'|tr -d '\r'
