more bash! regex substitution to crop trailing whitespaces

E K ekg_ab-FFYn/CNdgSA at public.gmane.org
Tue Dec 18 20:16:51 UTC 2007


--- Madison Kelly <linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org> wrote:

> Hi all,
> 
>    I am almost done this darn bash script of mine (insane as it
> is), but 
> I've got one hurdle I can't seem to get over... It *should* be the
> last 
> hurdle for me to finish this things and start the long process of 
> regaining some sanity.
> 
>    I read in key=value pairs from a file, which is easy enough. I
> can 
> cut off preceeding white spaces just fine with:
> 
> -=-=-=-=-
> if [[ "$line" =~ "(.*)=(.*)" ]]; then
> {
> 	# Assign the lhs (left-hand-side) to 'KEY', and the rhs to 'VAL'.
> 	KEY=${BASH_REMATCH[1]};
> 	VAL=${BASH_REMATCH[2]};
> 	echo ">> Pair - key: [${KEY}]	-	val: [${VAL}]."
> 
> 	# Leading spaces off of VAL
> 	if [[ "$VAL" =~ '(\s+)(.*)' ]]; then
> 	{
> 		VAL=${BASH_REMATCH[2]};
> 	}
> 	fi
> 
> 	# Trailing spaces off of VAL; FAILS!
> 	if [[ "$VAL" =~ '(.*)(\s+)$' ]]; then
> 	{
> 		VAL=${BASH_REMATCH[1]};
> 	}
> 	fi
> {
> fi
> -=-=-=-=-
> 
>    But I can't get trailing spaces cut off. The '(.*)(\s+)' pattern
> 
> doesn't match because it's greedy and matches to the end of the
> string. 
> This:
> 
> -=-=-=-=-
> echo "val: [${VAL/\s+/}]";
> -=-=-=-=-
> 
>    Doesn't match because '\s' is not treated as 'whitespace' and 
> '[:blank:]' seems to be plain out ignored...
> 
>    I've been reading and search and head->desk-ing for hours now...
> The 
> closest I've found, doc-wise, is this from 'man bash':
> 
> -=-=-=-=-
>         ${parameter/pattern/string}
>         ${parameter//pattern/string}
>                The  pattern is expanded to produce a pattern just
> as in
>                pathname  expansion.   Parameter  is  expanded  and 
> the
>                longest  match  of pattern against its value is
> replaced
>                with string.  In the first form, only the first
> match is
>                replaced.  The second form causes all matches of
> pattern
>                to be replaced with string.  If pattern begins  with
>  #,
>                it  must match at the beginning of the expanded
> value of
>                parameter.  If pattern begins with %, it must  match
>  at
>                the  end  of the expanded value of parameter.  If
> string
>                is null, matches of pattern are deleted and the  / 
> fol‐
>                lowing  pattern may be omitted.  If parameter is @
> or *,
>                the substitution operation is applied to each
> positional
>                parameter  in  turn,  and the expansion is the
> resultant
>                list.  If parameter is  an  array  variable 
> subscripted
>                with  @  or  *, the substitution operation is
> applied to
>                each member of the array in turn, and the  expansion
>  is
>                the resultant list.
> -=-=-=-=-
> 
>    However, I seem to be failing to grasp something...
> 
> help?  :)


Can you try 

VAL1=`echo $VAL`   # ` is the back quote

and use $VAL1 in what you will be using $VAL. If the trailing spaces
are recognized as such by the shell, the above assignment should
remove them.

Hope that helps,
EK

> 
> Madi
> --
> The Toronto Linux Users Group.      Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
> 



      ____________________________________________________ 
Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at
http://ca.toolbar.yahoo.com.
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list