[SPAM] - Re:more bash! regex substitution to crop trailing whitespaces - Email found in subject

jmacaranas-/DrkG639A0k at public.gmane.org jmacaranas-/DrkG639A0k at public.gmane.org
Tue Dec 18 20:43:24 UTC 2007


You can try this out..

while [[ $x = *[$' \t\n'] ]]; do x=${x%[$' \t\n']}; done

-----Original Message-----
From: owner-tlug-lxSQFCZeNF4 at public.gmane.org [mailto:owner-tlug-lxSQFCZeNF4 at public.gmane.org] On Behalf Of E K
Sent: Tuesday, December 18, 2007 3:17 PM
To: tlug-lxSQFCZeNF4 at public.gmane.org
Subject: [SPAM] - Re: [TLUG]: more bash! regex substitution to crop trailing whitespaces - Email found in subject


--- 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

--------------------------------------------------------------------------------------------------------
This message and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom it is
addressed. It may contain sensitive and private proprietary or legally
privileged information. No confidentiality or privilege is waived or
lost by any mistransmission. If you are not the intended recipient,
please immediately delete it and all copies of it from your system,
destroy any hard copies of it and notify the sender. You must not,
directly or indirectly, use, disclose, distribute, print, or copy any
part of this message if you are not the intended recipient. 
FXDirectDealer, LLC reserves the right to monitor all e-mail 
communications through its networks. Any views expressed in this 
message are those of the individual sender, except where the 
message states otherwise and the sender is authorized to state them.

Unless otherwise stated, any pricing information given in this message
is indicative only, is subject to change and does not constitute an
offer to deal at any price quoted. Any reference to the terms of
executed transactions should be treated as preliminary only and subject
to our formal confirmation. FXDirectDealer, LLC is not responsible for any
recommendation, solicitation, offer or agreement or any information
about any transaction, customer account or account activity contained in
this communication.

--
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