[Gllug] Stripping whitespace in bash script

Alex Sayle alexs at alphacomplex.info
Sat Aug 12 14:46:43 UTC 2006


On 12 Aug 2006, at 03:13BDT, Tethys wrote:

>
> Alex Sayle writes:
>
>> sed is your friend.
>
> A statement with which I wholeheartedly agree. Eventually, I'll
> even get around to putting the slides from my recent sed talk up
> on the web...

sounds nice, I do however think that sed script that is more than
4 lines long is usually better done in perl... none the less I'd
love to see the slides.

<snip />

> 	leto:~% word="some trailing    spaces     "
> 	leto:~% echo ":${word//%  */}:"
> 	:some trailing:
> 	leto:~% [[ "$word" =~ '.*[^ ]' ]]
> 	leto:~% echo ":$BASH_REMATCH:"
> 	:some trailing    spaces:

ahhh neat-bash ness. I wasn't aware of the regex stuff in bash.

Mildly off topic but zsh is my choice of shell an under zsh you
can do the following:

	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.


--alex

--
[----------------------------------------------------------------------]
  Alex Sayle                                     alexs at alphacomplex.info


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




More information about the GLLUG mailing list