Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #231247
| From | Emanuel Berg <moasenwood@zoho.eu> |
|---|---|
| Newsgroups | linux.debian.user |
| Subject | Re: URL to source file |
| Date | 2021-01-29 05:00 +0100 |
| Message-ID | <BCt6W-1Te-1@gated-at.bofh.it> (permalink) |
| References | (5 earlier) <BBQyC-3wL-3@gated-at.bofh.it> <BBQRZ-3Su-23@gated-at.bofh.it> <BBRkZ-41M-3@gated-at.bofh.it> <BBTmN-5bT-9@gated-at.bofh.it> <BC4ie-3Dx-1@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
Vincent Lefevre wrote:
> The zsh parameter expansion syntax is very cryptic (and the
> above command is far from being the worst).
>
>> so can you please explain that syntax or provide a URL
>> where it is explained? (Or, just a list with names and/or
>> examples should be sufficient, actually.)
>>
>> The M, f, quotes, colons, and hashes?
>
> Using the "units" example, we start with:
>
> print -r "$(aptitude show units)"
>
> The double quotes are necessary, as the zshexpn(1) man page
> says: for command substitution:
>
> "If the substitution is not enclosed in double quotes, the
> output is broken into words using the IFS parameter."
>
> But here, we want to keep the end of lines.
>
> Then one wants to get the "Version:" line, and one can do
> this via an array. The "(f)" will turn the aptitude output
> to an array with splitting at the end of lines (\n).
> This can be seen with
>
> for i in ${(f)"$(aptitude show units)"}; print -r "[$i]"
>
> Without the "(f)", one just gets one element.
>
> The ${name:#pattern} syntax on an array does filtering of
> elements matching the pattern. By default, matching array
> elements are removed. Here, we want the opposite: to keep
> the matching array elements. This is done with the "(M)"
> flag. Here, the pattern to use is "Version:*", which matches
> lines that start with "Version:", the * character matching
> any sequence of characters, like in filename generation
> (a.k.a. globbing). So,
>
> print -r ${(M)${(f)"$(aptitude show units)"}:#Version:*}
>
> gives on my machine:
>
> Version: 2.21-1
>
> Then, we want to remove the "Version: " prefix. This is done
> with the ${name#pattern} syntax. One could use the "Version:
> " pattern, but "* " is shorter and safe since there should
> be a single space. Hence
>
> print -r ${${(M)${(f)"$(aptitude show units)"}:#Version:*}#* }
>
> Note: instead of nested substitutions, one can use
> intermediate variables, which can make the code easier
> to understand.
Thank you, very useful :)
--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal
Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 10:30 +0100
Re: URL to source file <tomas@tuxteam.de> - 2021-01-27 10:40 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 11:00 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 11:10 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 11:20 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 11:50 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 12:10 +0100
Re: URL to source file Vincent Lefevre <vincent@vinc17.net> - 2021-01-27 12:40 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 14:50 +0100
Re: URL to source file Vincent Lefevre <vincent@vinc17.net> - 2021-01-28 02:30 +0100
Re: URL to source file Greg Wooledge <wooledg@eeg.ccf.org> - 2021-01-28 14:20 +0100
Re: URL to source file Vincent Lefevre <vincent@vinc17.net> - 2021-01-28 15:40 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-29 05:00 +0100
Re: URL to source file Andrei POPESCU <andreimpopescu@gmail.com> - 2021-01-27 17:10 +0100
Re: URL to source file <tomas@tuxteam.de> - 2021-01-27 11:30 +0100
Re: URL to source file Stefan Monnier <monnier@iro.umontreal.ca> - 2021-01-27 16:00 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 16:20 +0100
Re: URL to source file Emanuel Berg <moasenwood@zoho.eu> - 2021-01-27 16:30 +0100
csiph-web