zsh-users
 help / color / mirror / code / Atom feed
* Re: Extracting Informations from strings
@ 2004-06-22  9:02 Elisabetta Riboldi
  0 siblings, 0 replies; 4+ messages in thread
From: Elisabetta Riboldi @ 2004-06-22  9:02 UTC (permalink / raw)
  To: zsh-users


PLS  TAKE  OFF  MY  ADDRESS   FROM  YOUR  DISTRIBUTION  LIST
THANKS  AND  BEST  REGARDS

.

E. Riboldi
Recupero Crediti - Multivendor Services srl
MI BAS 009 / 20060  Basiano - Via Pirandello 1
tel. 02.596.25624 - fax 02.596.20555
e-mail: Elisabetta_Riboldi@multivendor.it


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Extracting Informations from strings
  2004-06-21  9:33 ` Peter Stephenson
@ 2004-06-21 16:28   ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2004-06-21 16:28 UTC (permalink / raw)
  To: zsh-users

On Mon, 21 Jun 2004, Peter Stephenson wrote:

> Stefan =?iso-8859-1?Q?Reich=F6r?= wrote:
> >   Battery 1: charging, 95%, charging at zero rate - will never fully charge.
> > 
> > I want to extract the "95%" from the output above.
> 
> setopt extendedglob
> local match mbegin mend
> if [[ $string = (#b)"Battery "*" charging, "(<->)"%"* ]]; then
>   print "Charge is ${match[1]}%"
> fi

To do this without extendedglob and backreferences is, I think, simpler:

	print Charge is ${(M)${(M)string%%<->%,*}##<->%}

This is (where <-> matches a string of digits):

	${(M)string%%<->%,*}	Longest suffix beginning with <->%
	${(M)...##<->%}		Longest prefix of that matching <->%

I'd only resort to backreferences when the substring to be extracted is
ambiguous (e.g. "... charging, 95%, charging at 17%, ...") or when
extracting multiple fields (e.g. both the battery number and the charge
percentage).


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Extracting Informations from strings
  2004-06-21  7:02 Stefan Reichör
@ 2004-06-21  9:33 ` Peter Stephenson
  2004-06-21 16:28   ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2004-06-21  9:33 UTC (permalink / raw)
  To: zsh-users

Stefan =?iso-8859-1?Q?Reich=F6r?= wrote:
> Hi!
> 
> I want to parse the output from acpi:
>   Battery 1: charging, 95%, charging at zero rate - will never fully charge.
> 
> I want to extract the "95%" from the output above.
> 
> How can I achieve this from within zsh?

Within zsh, you need something like (assuming the output is in $string):

setopt extendedglob
local match mbegin mend
if [[ $string = (#b)"Battery "*" charging, "(<->)"%"* ]]; then
  print "Charge is ${match[1]}%"
fi

It's up to you how closely you want to match the output, I've matched a
lot of it with *'s.

This works from zsh 4.0.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Extracting Informations from strings
@ 2004-06-21  7:02 Stefan Reichör
  2004-06-21  9:33 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Reichör @ 2004-06-21  7:02 UTC (permalink / raw)
  To: zsh-users

Hi!

I want to parse the output from acpi:
  Battery 1: charging, 95%, charging at zero rate - will never fully charge.

I want to extract the "95%" from the output above.

How can I achieve this from within zsh?


Stefan.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-06-22  8:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-22  9:02 Extracting Informations from strings Elisabetta Riboldi
  -- strict thread matches above, loose matches on Subject: below --
2004-06-21  7:02 Stefan Reichör
2004-06-21  9:33 ` Peter Stephenson
2004-06-21 16:28   ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).