From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Anssi Saari <as@sci.fi>
Cc: zsh-users@zsh.org
Subject: Re: Confused about splitting
Date: Sun, 31 Mar 2019 13:12:04 +0000 [thread overview]
Message-ID: <20190331131204.mpyahlfvy2dxe63f@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <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
next prev parent reply other threads:[~2019-03-31 13:12 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-31 9:06 Anssi Saari
2019-03-31 12:58 ` Daniel Shahaf
2019-03-31 17:56 ` Anssi Saari
2019-03-31 13:12 ` Daniel Shahaf [this message]
2019-04-01 9:07 ` Anssi Saari
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190331131204.mpyahlfvy2dxe63f@tarpaulin.shahaf.local2 \
--to=d.s@daniel.shahaf.name \
--cc=as@sci.fi \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).