ed s/a*/./g description

Eric Gisin eric at mks.com
Sat Feb 9 07:35:50 AEST 1991


Submitted-by: eric at mks.com (Eric Gisin)

Does anyone have an complete description of how s///g and awk's gsub
handle the special case of an empty pattern match?
The System V documents and POSIX.2 don't cover this.

For example, the command s/[a-z]*/./g does the following on System V ed:
	-bug-	becomes .-.-.
The following algorithm produces an extra dot:
	-bug-	becomes	.-..-.

gsub(pattern, replace, src, dst)
	while (1) {
		find next pattern in src
		copy part before match from src to dst
		copy replace to dst
		advance src to end of match
		if (at end of src)	break;
		/* special case for empty match */
		if (match was empty)
			*dst++ = *src++;
	}


Volume-Number: Volume 22, Number 118



More information about the Comp.std.unix mailing list