Bug 20449

Summary: strange behaviour of sed 's/ */ /' w/ or w/o g
Product: Base System Reporter: clefevre <clefevre>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description clefevre 2000-08-07 04:50:00 UTC
	well, sed(1) substitutions seems to be very buggy. see below.
	maybe it's regex(3), don't know yet.

Fix: 

n/a
How-To-Repeat: 
	$ echo 'a  string with  spaces' | sed 's/ */ /'
	 a  string with  spaces

	a space is prepended instead of replacing the first 2 spaces w/ one.

	$ echo 'a  string with  spaces' | sed 's/ */ /g'
	 a s t r i n g w i t h s p a c e s

	all empty strings are matched instead of matching spaces

	$ echo 'a  string with  spaces' | sed 's/g*/x/g'
	xax x xsxtxrxixnx xwxixtxhx x xsxpxaxcxexsx

	$ echo 'a  string with  spaces' | sed 's/z*/x/g'
	xax x xsxtxrxixnxgx xwxixtxhx x xsxpxaxcxexsx

	in fact, not only space substitutions are very buggy.
Comment 1 Gregory Bond 2000-08-07 05:14:26 UTC
> 	$ echo 'a  string with  spaces' | sed 's/ */ /'
> 	 a  string with  spaces

This is exactly what you've asked for:  the first place which matches / */
(i.e. 0 or more spaces, i.e. the empty string at the beginning of the line) is
replaced with a space.  

Similarly with the other 3 examples.

Check up the meaning of "*" in regex(3).
Comment 2 Sheldon Hearn freebsd_committer freebsd_triage 2000-08-07 09:58:24 UTC
State Changed
From-To: open->closed

You've shown us some examples of sed(1) doing the right thign. 
If you need help with regular expressions, there are some pretty 
keen beans on the freebsd-questions mailing list. :-)