zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: subsitutions and beginning of lines.
Date: Sun, 11 Oct 2015 21:09:02 +0100	[thread overview]
Message-ID: <20151011210902.566de251@ntlworld.com> (raw)
In-Reply-To: <561AB49A.4060801@eastlink.ca>

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


  reply	other threads:[~2015-10-11 20:09 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 [this message]
2015-10-11 23:05   ` Ray Andrews
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=20151011210902.566de251@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --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).