Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: subsitutions and beginning of lines.

On Sun, 11 Oct 2015 12:12:26 -0700
Ray Andrews <rayandrews@xxxxxxxxxxx> 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.


Messages sorted by: Reverse Date, Date, Thread, Author