Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Confused about splitting
- X-seq: zsh-users 23895
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: Anssi Saari <as@xxxxxx>
- Subject: Re: Confused about splitting
- Date: Sun, 31 Mar 2019 13:12:04 +0000
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= daniel.shahaf.name; h=date:from:to:cc:subject:message-id :references:mime-version:content-type:content-transfer-encoding :in-reply-to; s=fm2; bh=cs6/hkmXJ1yk+rEOdNgbMvjJxsyaE1s1gnBzPI2r Cmo=; b=eSNRwiwFLsgjbGwAtAPEzLApL4kySjKYlyGaKbHDQAvCTiBU69EqS2Yv wM5t+0dUmYR6JiVK4jEvxKlYBbQJM0MenjBTBn3e/xduVIcwmSy+Qpm+RltVbp1m efzwjzyoPaDVzcHk8xow5VQgb8/b/aE0n8hzEOei35HUxIfDvbRhXLeCegBiz9It 8E79LN8pCJCW3zJWwHZbgTNhX9vN4IEczoWV6uFGLJekqQnM+UOhCx1gnAaoZhrB TtzOGdX7HIna9ToAQHrSg/4jclnh49kWo8fmEu8Fb9RDz0vcWiyjniVs0f2bsxsA kg87ZOZot+6iZGf7eZDYVMcQgGtGWA==
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=cs6/hkmXJ1yk+rEOdNgbMvjJxsyaE1s1gnBzPI2rC mo=; b=ZWI+t+goijJ5cc2Px9sjYyGmpHy1hSjAz+UTr3R5Aiyyhz/Jffh6gXdPE uOqX1JnoPNXbb9QuSo8Gm4auM+mf4TNmBlQ11ElYFMsxcRBz4aWKRkmdbegzKe9S feZPAj5WWrvGSjfq4Ywlh7jZQW/FhiAknp3/03/xST2r+vSEHgwbEZ4fNMtQ7gOD LnoX5WQpnPx2hRBzCtgUzFywmT1hjXiw9/nQULlrkChAjE10S9ZCG17+JXPw8AWO osoWhfj9RVWq9nxQZn4FVG7ivTOUPv1gmtAOc42T/baiC24cO1/XwdAoZl0vFW10 o9wSvxohCG1F+DGrh1whrg+3mV2mg==
- In-reply-to: <vg3k1gf4dmf.fsf@coffee.modeemi.fi>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <vg3k1gf4dmf.fsf@coffee.modeemi.fi>
Anssi Saari wrote on Sun, Mar 31, 2019 at 12:06:16 +0300:
> foofunc3 () {
> if [[ $# -ge 1 ]]
> then
> if=$1
> else
> if=eno1
> fi
> while read g
> do
> if [[ $g =~ $if ]]
> then
> first=${=g}
> break
> fi
> done < /proc/net/dev
>
> echo $first
> echo length of first is $#first
> }
Here's another solution:
1 % () {
2 : ${1:=enp2s0}
3 local -a net_dev_lines=( "${(@f)"$(</proc/net/dev)"}" )
4 local line=${${(M)net_dev_lines:#*$1*}[1]}
5 print -r -- $=line
6 print -r -- ${#${=line}}
7 }
8 17
9 %
Line 2 sets $1 to "enp2s0" if it's not already set.
Line 3 slurps the target file (the «$(<…)») and splits it into lines (the «${(f)}»).
Line 4 greps it (that's the :# and the (M) together) and takes the first result
(that's the outer «${…[1]}» construct).
Line 6 splits it, takes the length of the intermediate array, and prints that.
Cheers,
Daniel
Messages sorted by:
Reverse Date,
Date,
Thread,
Author