在zsh中的大括号扩展 – 如何将两个列表连接起来以进行文件选择?

huangapple go评论49阅读模式
英文:

Brace expansion in zsh - how to concatenate two lists for file selection?

问题

zsh中,可以使用表达式{nx..ny},例如选择文件夹中从x到y的文件。

例如,{1..50}选择从1到50的项目、文件等。

如何将两个大括号扩展连接成一个?示例:我想选择{1..50}{60..100},使其输出为一个结果。

英文:

In zsh, one can create an expression of {nx..ny}, for example to select files x to y inside a folder.

For example, {1..50} selects items, files, etc. from 1 to 50.

How can I concatenate two two brace expansions into one?
Example: I would like to select {1..50} and {60..100} for one and the same output.

答案1

得分: 3

你可以嵌套大括号扩展,因此这将起作用:

> print {{1..50},{60..100}}
1 2 3 (很多数字) 49 50 60 61 (更多数字) 99 100

大括号扩展支持列表和序列,并可包含在字符串中:

> print -l file{A,B,WORD,{R..T}}.txt
fileA.txt
fileB.txt
fileWORD.txt
fileR.txt
fileS.txt
fileT.txt

请注意,大括号扩展不是通配模式。{n..m} 扩展将包括开始和结束值之间的每个值,无论是否存在该名称的文件。要在文件夹中查找文件,<-> 通配表达式通常效果更好:

> touch 2 3 55 89
> ls -l <1-50> <60-100>
-rw-r--r--  1 me  grp  0 Feb 18 06:52 2
-rw-r--r--  1 me  grp  0 Feb 18 06:52 3
-rw-r--r--  1 me  grp  0 Feb 18 06:52 89
英文:

You can nest brace expansions, so this will work:

&gt; print {{1..50},{60..100}}
1 2 3 (lots of numbers) 49 50 60 61 (more numbers) 99 100

Brace expansions support lists as well as sequences, and can be included in strings:

&gt; print -l file{A,B,WORD,{R..T}}.txt
fileA.txt
fileB.txt
fileWORD.txt
fileR.txt
fileS.txt
fileT.txt

Note that brace expansions are not glob patterns. The {n..m} expansion will include every value between the start and end values, regardless of whether a file exists by that name. For finding files in folders, the &lt;-&gt; glob expression will usually work better:

&gt; touch 2 3 55 89
&gt; ls -l &lt;1-50&gt; &lt;60-100&gt;
-rw-r--r--  1 me  grp  0 Feb 18 06:52 2
-rw-r--r--  1 me  grp  0 Feb 18 06:52 3
-rw-r--r--  1 me  grp  0 Feb 18 06:52 89

huangapple
  • 本文由 发表于 2023年2月18日 17:31:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定