Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Extracting Informations from strings



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@xxxxxxx>                  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
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author