[Gllug] Stripping whitespace in bash script

Tethys sta296 at astradyne.co.uk
Sat Aug 12 15:57:48 UTC 2006


Alex Sayle writes:

>	zsh$setopt extendedglob
>	zsh$word="some  railing   spaces     "
>	zsh$echo ":${word//% ##/}:"
>	:some  railing   spaces:
>
>which matches one of more space from the end of the value, it does
>however need the extendedglob feature turned on to work.

Bah! I knew I should have been able to do it with extended globbing
in bash, without resorting to regexp matching, but it was 3am, and I
couldn't think straight. In the cold light of day:

	leto:~% word="some trailing spaces     "
	leto:~% shopt -s extglob
	leto:~% echo ":${word/%*( )}:"
	:some trailing spaces:

Or if your whitespace might contain tabs as well as spaces:

	leto:~% word="some trailing spaces     "
	leto:~% shopt -s extglob
	leto:~% echo ":${word/%*([[:blank:]])}:"
	:some trailing spaces:

Tet
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list