Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: read options to slurp empty lines ?
- X-seq: zsh-users 23425
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Marc Chantreux <eiro@xxxxxxxxx>
- Subject: Re: read options to slurp empty lines ?
- Date: Fri, 1 Jun 2018 18:17:19 -0700
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=XoIjxrvoy/nswg1cj6Rzbo83F+23bijLqQ/Fk93k6VQ=; b=hskVDAUvXLoQ1YXZi9IVMO6rXRPHGpIG5XfFx6M9xotsaMT+peANCilCZ/vBIJ7Uqv MC3+f2zJib2U+atVOHo+doB/K+yw/i099y7K+MtLU0j45DbS+PLfwkC+HOG5RKneD+KS alvBFrvyvNpuP1UVZbY7/Hy+EiLuaC19VzroSH9l+M49AOJ13oa3Axs1WXP4n2gEF0pZ NvTl5P0GGumz+GLg+3DfF+KD0WNHNRI/XDtlvvR9wQhF+BZ/XcryMIxv9pDT6RncR06l pKuPp1d48V4+hkpvfmVZE5fIEknNE6Iae6bYRXNBLsonPhyd6wi6J2KJUf9ZzF0ItWjf 5BJw==
- In-reply-to: <20180601214714.GA11281@prometheus.u-strasbg.fr>
- 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: <20180601214714.GA11281@prometheus.u-strasbg.fr>
On Fri, Jun 1, 2018 at 2:47 PM, Marc Chantreux <eiro@xxxxxxxxx> wrote:
>
> i use to have slurp function made like this
>
> slurp () IFS=$'\n' read -r -d ' -A $1
I presume that to be a typo or cut-and-paste error because you have an
unmatched single quote there.
> i recently saw that
>
> print -l '' bar bang '' | slurp them
> # got them=( bar bang )
> # expected them=( '' bar bang '' )
For the confused or those with proportional fonts, '' above is two
single quotes, not a double quote, so there should be four lines
output by "print -l".
I get
% typeset -p them
typeset -a them=( bar bang '' )
The behavior of "read" is to collapse multiple consecutive occurrences
of $IFS characters into a single one. Because an empty string
followed by a newline looks like nothing more than a newline, all
those lines collapse. [The single empty element I got at the end is
there not because there were trailing empty lines, but because end of
input was reached without finding the delimiter (this is not standard
so probably ought to be controlled by POSIX_BUILTINS or something).]
So for example:
% print -l x '' '' '' bing bong '' '' '' y | slurp
% typeset -p reply
typeset -a reply=( x bing bong y '' )
Same thing happens in e.g. bash (except without the final empty
element, see above).
> i tried to patch it using some read options ... did i miss something using read builtin ?
Only that there's no simple way to make it do what you want. :-)
You're probably looking for this:
slurp() {
local REPLY
IFS= read -d '' &&
set -A ${1:-reply} "${(@f)REPLY}"
}
Messages sorted by:
Reverse Date,
Date,
Thread,
Author