Powershell equivalent of Bash Brace Expansion for generating lists/arrays

前端 未结 3 540
走了就别回头了
走了就别回头了 2020-12-30 05:11

When writing a Bash script you can use brace expansion to quickly generate lists:

\"Bash

3条回答
  •  臣服心动
    2020-12-30 05:34

    I have a way to do it using int's tostring method. The '000' at the end is a special format code. 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.

    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.

    10..15 | % tostring x
    
    a
    b
    c
    d
    e
    f
    

    There's always -replace, which also works on arrays. '^' 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...

提交回复
热议问题