[英]Powershell equivalent of Bash Brace Expansion for generating lists/arrays
When writing a Bash script you can use brace expansion to quickly generate lists:在编写 Bash 脚本时,您可以使用 大括号扩展来快速生成列表:
What is the simplest way to generate a similar list in Powershell?在 Powershell 中生成类似列表的最简单方法是什么? I can use the .. or , operators to generate an array , but how can I prefix the items with a static string literal?
我可以使用 .. 或 , 运算符来生成一个数组,但是如何使用静态字符串文字作为项目的前缀?
PS C:\Users\gb> 1..5
1
2
3
4
5
PS C:\Users\gb> "test"+1..5
test1 2 3 4 5
PS C:\Users\gb> "test","dev","prod"
test
dev
prod
PS C:\Users\gb> "asdf"+"test","dev","prod"
asdftest dev prod
PS C:\> "test","dev","prod" | % { "server-$_" }
server-test
server-dev
server-prod
PS C:\> 1..5 | % { "server{0:D2}" -f $_ }
server01
server02
server03
server04
server05
PS C:\> 1..5 | % { "192.168.0.$_" }
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
Note that %
is an alias for the ForEach-Object
cmdlet.请注意,
%
是ForEach-Object
cmdlet 的别名。
I'm hoping to be proven wrong here, but I don't believe there is a way to do it exactly like with bash, or with as few keystrokes.我希望能在这里证明是错误的,但我不相信有一种方法完全一样使用bash,或做它用尽可能少的按键。
You can iterate over the list by piping it through a foreach-object
to achieve the same result though.您可以通过通过
foreach-object
管道来遍历列表以实现相同的结果。
1..5 | foreach-object { "test" + $_ }
Or using the shorthand:或者使用简写:
1..5 | %{"test$_"}
In both cases ( %
is an alias for foreach-object
), the output is:在这两种情况下(
%
是foreach-object
的别名),输出为:
test1
test2
test3
test4
test5
Note: if you're building this into a script for publishing/distribution/reuse, use the more verbose foreach-object
, not the shorthand %
- for readability.注意:如果您要将其构建到用于发布/分发/重用的脚本中,请使用更详细的
foreach-object
,而不是速记%
- 以提高可读性。
I have a way to do it using int's tostring method .我有一种方法可以使用int 的 tostring 方法来做到这一点。 The '000' at the end is a special format code.
最后的'000'是一个特殊的格式代码。 It always pads to the right number of zeros.
它总是填充到正确数量的零。 You can also use wildcards with method names like t*g if you really want to be terse and mysterious.
如果您真的想要简洁和神秘,您还可以使用带有方法名称的通配符,例如 t*g。
1..10 | % tostring computer000
computer001
computer002
computer003
computer004
computer005
computer006
computer007
computer008
computer009
computer010
1..10 | % t*g 192\.168\.1\.0
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
'x' is also a format code for hex printing. 'x' 也是十六进制打印的格式代码。
10..15 | % tostring x
a
b
c
d
e
f
There's always -replace, which also works on arrays.总是有 -replace,它也适用于数组。 '^' is regex for 'beginning of line'.
“^”是“行首”的正则表达式。 Use '$' instead for 'end of line'.
使用“$”代替“行尾”。
(echo test dev prod) -replace '^','server-'
server-test
server-dev
server-prod
Hah, I never tried this before.哈,我以前从来没有试过这个。
(echo test dev prod) -replace (echo ^ server-)
server-test
server-dev
server-prod
Maybe they could do that brace expansion in powershell 8...也许他们可以在powershell 8中进行大括号扩展......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.