zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: zsh-users@sunsite.auc.dk
Subject: Fun zsh trick for today
Date: Sat, 10 Jun 2000 22:50:51 +0000	[thread overview]
Message-ID: <1000610225052.ZM32045@candle.brasslantern.com> (raw)

Some of you are probably familiar with Perl's "grep" function; given a
regular expression and an array, it returns all the elements of the array
that match the expression.  A common Perl idiom is

	open(FILE, "<somefile");
	@result = grep(/pat/, <FILE>);

Where <FILE> slurps somefile into memory as an array of lines.

Did you know that zsh can do that, too?

If you load the mapfile module:

	zmodload -i zsh/mapfile

Then you can write Perl's open(FILE, "<somefile") as

	$mapfile[somefile]

And you can write <FILE> as

	${(f)mapfile[somefile]}

with the caveat that blank lines are stripped out.

(If you're using zsh 3.0.x or can't load the mapfile module, you can do the
same thing with "${(f)$(<somefile)}".  It's just not quite as efficient,
and you have to remember to use the double quotes around it.)

You can write grep(/pat/, array) as

	${(M)array:#*pat*}

with the caveat that pat is a glob pattern -- but with extendedglob set,
zsh's glob patterns are full regular expressions, although with a non-sed-
like syntax because of the different special meaning of '*'.

So finally, @result = grep(/pat/, <FILE>) is

	result=(${(M)${(f)mapfile[somefile]}:#*pat*})

Try, for example, ${(M)#${(f)mapfile[ChangeLog]}:#*Sven*} to see that there
are 1023 mentions of Sven's name in zsh's ChangeLog file.  (I only managed
265 "Bart"s; in fact, there are only 475 lines with the word "zsh"!)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


             reply	other threads:[~2000-06-10 22:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-06-10 22:50 Bart Schaefer [this message]
     [not found] <no.id>
2000-06-14 13:43 ` Matthias Kopfermann
2000-06-14 13:57   ` Clint Adams
2000-06-14 14:09     ` Ollivier Robert
2000-06-14 15:02       ` Fletch
2000-06-14 14:15     ` Peter Stephenson
2000-06-14 14:21       ` Peter Stephenson
2000-06-14 14:27       ` Clint Adams
2000-06-14 14:37       ` Bart Schaefer
2000-06-14 15:33 ` Matthias Kopfermann
2000-06-14 15:52   ` Peter Stephenson
2000-06-14 16:31 ` Matthias Kopfermann

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=1000610225052.ZM32045@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=zsh-users@sunsite.auc.dk \
    /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).