zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: subsitutions and beginning of lines.
Date: Sun, 11 Oct 2015 16:05:19 -0700	[thread overview]
Message-ID: <561AEB2F.8030808@eastlink.ca> (raw)
In-Reply-To: <20151011210902.566de251@ntlworld.com>

On 10/11/2015 01:09 PM, Peter Stephenson wrote:
> On Sun, 11 Oct 2015 12:12:26 -0700
> Ray Andrews <rayandrews@eastlink.ca> wrote:
>> ${var// /}
>>
>> When doing that sort of substitution, is is possible to test for newlines?
>> I'm playing with a function that grabs history and I'm trying to strip
>> off the leading numbers.  At one point I have the output captured in a
>> variable and various forms of substitution come close, but the substitution
>> treats the entire variable as one entity whereas I want the substitution
>> to be performed fresh on each new line of output.  I can't find any
>> specific newline character.  OTOH if the variable was an array there'd
>> probably be some way of processing each line as a separate entity, which
>> would serve fine. I know I'm close, but I can't quite bag it.
> If I'm following, you are indeed nearly there: you need to split the
> variable to an array on newlines, while not splitting on anything else.
> To do that, you quote the array, then force the splitting which ensures it
> only gets split on what you tell it and not other other random spaces:
>
> var="1:one two
> 2:three four"
> print -l -- ${(f)"${var}"}
>
> The -l prints one element per line for clarity.
>
> You've now got the lines you want as if they were array elements.  You
> can then surround that substitution with the sort you first thought of.
> For example, to strip the "number:",
>
> print -l -- ${${(f)"${var}"}##<->:}
>
> where, as you now know, <-> matches any set of digits.
>
> pws
Thanks Peter,  I just came up with this:

     echo "${(F)var[@]//#???????/}"

... it seems the numeric leader is always the same width, so mine is 
more stupidly
linear, but it was my enlightenment as to '<->' that got me going with this.
I pretty much understand it; the trick was to get the substitution on 
each element.
And get the newline as the separator.  I'll study your version too.

BTW, I just rediscovered your:

A User's Guide to the Z-Shell
Peter Stephenson
2003/03/23

In the fog of war I'd considered it to be 'the manual' back when I first 
started,
then when I started reading the real manual I had this powerful 
sensation that
the manual had gone downhill.  Now I'm realizing that they're not the same
tome after all and I must say that yours is a pleasure to read which 
can't be
said of the manual.  Is there an update?








  reply	other threads:[~2015-10-11 23:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-11 19:12 Ray Andrews
2015-10-11 20:09 ` Peter Stephenson
2015-10-11 23:05   ` Ray Andrews [this message]
2015-10-12  9:34     ` Peter Stephenson
2015-10-12 15:44       ` Ray Andrews
2015-10-12 17:07         ` Ray Andrews
2015-10-13  2:29           ` Bart Schaefer
2015-10-13  2:50             ` Ray Andrews
2015-10-13  5:03               ` Bart Schaefer
2015-10-13  5:24                 ` Ray Andrews
2015-10-13 19:58                   ` Bart Schaefer
2015-10-13 21:42                     ` Ray Andrews
2015-10-14  0:23                       ` Bart Schaefer
2015-10-14  5:03                         ` Ray Andrews
2015-10-12  2:26 ` Daniel Shahaf
2015-10-12 15:53   ` Ray Andrews
2015-10-12 16:42     ` Peter Stephenson

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=561AEB2F.8030808@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --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).