* Extracting Informations from strings
@ 2004-06-21 7:02 Stefan Reichör
2004-06-21 7:24 ` [zsh] " Artur Penttinen
2004-06-21 9:33 ` Peter Stephenson
0 siblings, 2 replies; 6+ 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] 6+ messages in thread
* Re: [zsh] Extracting Informations from strings
2004-06-21 7:02 Extracting Informations from strings Stefan Reichör
@ 2004-06-21 7:24 ` Artur Penttinen
2004-06-21 18:35 ` Vincent Stemen
2004-06-21 9:33 ` Peter Stephenson
1 sibling, 1 reply; 6+ messages in thread
From: Artur Penttinen @ 2004-06-21 7:24 UTC (permalink / raw)
To: zsh-users
On Mon, 21 Jun 2004, Stefan [iso-8859-1] ReichЖr wrote:
> Date: Mon, 21 Jun 2004 09:02:44 +0200
> From: "Stefan [iso-8859-1] ReichЖr" <xsteve@riic.at>
> To: zsh-users@sunsite.dk
> Subject: [zsh] Extracting Informations from strings
>
> 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?
# print "Battery 1: charging, 95%, charging at zero rate - will never fully charge." | cut -d, -f2
95%
#
--
artur penttinen
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [zsh] Extracting Informations from strings
2004-06-21 7:24 ` [zsh] " Artur Penttinen
@ 2004-06-21 18:35 ` Vincent Stemen
0 siblings, 0 replies; 6+ messages in thread
From: Vincent Stemen @ 2004-06-21 18:35 UTC (permalink / raw)
To: zsh-users
On Mon, Jun 21, 2004 at 10:24:16AM +0300, Artur Penttinen wrote:
> On Mon, 21 Jun 2004, Stefan [iso-8859-1] Reich?r wrote:
>
> > Date: Mon, 21 Jun 2004 09:02:44 +0200
> > From: "Stefan [iso-8859-1] Reich?r" <xsteve@riic.at>
> > To: zsh-users@sunsite.dk
> > Subject: [zsh] Extracting Informations from strings
> >
> > 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?
>
> # print "Battery 1: charging, 95%, charging at zero rate - will never fully charge." | cut -d, -f2
> 95%
> #
>
> --
> artur penttinen
You can also do this directly without calling cut.
output="Battery 1: charging, 95%, charging at zero rate - will never fully charge."
output=${${=output}[4]} # Split $output and extract the 4th field
print ${output%,} # Remove the comma
--
Vincent Stemen
Avoid the VeriSign/Network Solutions domain registration trap!
Read how Network Solutions (NSI) was involved in stealing our domain name.
http://www.InetAddresses.net
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Extracting Informations from strings
2004-06-21 7:02 Extracting Informations from strings Stefan Reichör
2004-06-21 7:24 ` [zsh] " Artur Penttinen
@ 2004-06-21 9:33 ` Peter Stephenson
2004-06-21 16:28 ` Bart Schaefer
1 sibling, 1 reply; 6+ 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] 6+ 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; 6+ 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] 6+ messages in thread
* Re: Extracting Informations from strings
@ 2004-06-22 9:02 Elisabetta Riboldi
0 siblings, 0 replies; 6+ 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] 6+ messages in thread
end of thread, other threads:[~2004-06-22 8:57 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-21 7:02 Extracting Informations from strings Stefan Reichör
2004-06-21 7:24 ` [zsh] " Artur Penttinen
2004-06-21 18:35 ` Vincent Stemen
2004-06-21 9:33 ` Peter Stephenson
2004-06-21 16:28 ` Bart Schaefer
2004-06-22 9:02 Elisabetta Riboldi
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).