Re: sh parameter substitution problem

2009-02-11 Thread eculp

Quoting Ian Smith smi...@nimnet.asn.au:


I'm getting nowhere trying to parse out IP addresses from strings of
this form in /bin/sh, which have been awk'd out of 'tail named.run':

 addr='195.68.176.4#1440:'
 addr='195.68.176.4#16811:'
 addr='195.68.176.4#276:'

sh(1) in hand, I've tried:

 ip=${addr:%#*}
 ip=${addr:%%#*}
 ip=${addr:%[#]*}
 ip=${addr:%%[#]*}

but all of these report './testbit: 7: Syntax error: Bad substitution'

How can I split these strings to exclude the '#' and all beyond,
preferably using sh syntax, at least without resorting to perl?


sed would work.  Something like

for i in addr='195.68.176.4#1440:' addr='195.68.176.4#16811:'  
addr='195.68.176.4#276:'

do
echo $i | sed 's/#.*//'
done

of course using the echo line in your script or something similar.   
I'm sure there are many simple or simpler solutions.


ed



Please cc me as I'm only subscribed to the digest.

TIA, Ian
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org





Failure is the opportunity to begin again more intelligently.
Fracaso es la oportunidad de reiniciar con mas inteligencia  Henry Ford
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: sh parameter substitution problem

2009-02-11 Thread Giorgos Keramidas
On Wed, 11 Feb 2009 21:47:17 +1100 (EST), Ian Smith smi...@nimnet.asn.au 
wrote:
 I'm getting nowhere trying to parse out IP addresses from strings of 
 this form in /bin/sh, which have been awk'd out of 'tail named.run':

  addr='195.68.176.4#1440:'
  addr='195.68.176.4#16811:'
  addr='195.68.176.4#276:'

 sh(1) in hand, I've tried:

  ip=${addr:%#*}
  ip=${addr:%%#*}
  ip=${addr:%[#]*}
  ip=${addr:%%[#]*}

 but all of these report './testbit: 7: Syntax error: Bad substitution'

 How can I split these strings to exclude the '#' and all beyond, 
 preferably using sh syntax, at least without resorting to perl?

Remove the ':' part and quote the text to avoid parsing '#' as a comment
delimiter:

$ addr='195.68.176.4#1440:'
$ echo ${addr%#*}
195.68.176.4

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: sh parameter substitution problem

2009-02-11 Thread Jonathan McKeown
On Wednesday 11 February 2009 12:47:17 Ian Smith wrote:
 I'm getting nowhere trying to parse out IP addresses from strings of
 this form in /bin/sh, which have been awk'd out of 'tail named.run':

  addr='195.68.176.4#1440:'
  addr='195.68.176.4#16811:'
  addr='195.68.176.4#276:'

 sh(1) in hand, I've tried:

  ip=${addr:%#*}
  ip=${addr:%%#*}
  ip=${addr:%[#]*}
  ip=${addr:%%[#]*}

 but all of these report './testbit: 7: Syntax error: Bad substitution'

Take out the : in the parameter expansion.

$ addr='195.68.176.4#1440:'; ip=${addr%#*}; echo $ip
195.68.176.4

: is for supplying default values or an error for unset variables.

Jonathan
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: sh parameter substitution problem

2009-02-11 Thread Ian Smith
On Wed, 11 Feb 2009, Giorgos Keramidas wrote:
  On Wed, 11 Feb 2009 21:47:17 +1100 (EST), Ian Smith smi...@nimnet.asn.au 
  wrote:
   I'm getting nowhere trying to parse out IP addresses from strings of 
   this form in /bin/sh, which have been awk'd out of 'tail named.run':
  
addr='195.68.176.4#1440:'
addr='195.68.176.4#16811:'
addr='195.68.176.4#276:'
  
   sh(1) in hand, I've tried:
  
ip=${addr:%#*}
ip=${addr:%%#*}
ip=${addr:%[#]*}
ip=${addr:%%[#]*}
  
   but all of these report './testbit: 7: Syntax error: Bad substitution'
  
   How can I split these strings to exclude the '#' and all beyond, 
   preferably using sh syntax, at least without resorting to perl?
  
  Remove the ':' part and quote the text to avoid parsing '#' as a comment
  delimiter:
  
  $ addr='195.68.176.4#1440:'
  $ echo ${addr%#*}
  195.68.176.4

Thankyou Giorgos,

just before yours arrived I'd twigged that the ':' was wrong there, and 
tried ip=${addr%#*} which worked fine.  I guess # within ${..} doesn't 
get taken as a comment .. which makes sense or these would always need 
to be double-quoted.

cheers, Ian
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: sh parameter substitution problem

2009-02-11 Thread Ian Smith
Thanks Ed - good old sed - and Jonathan too, now the digest's here.

  195.68.176.4

Lest anyone get the wrong idea: that's a victim, perpetrator unknown.

cheers, Ian
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org