Powershell中的*Nix'cut'命令相当于什么?

Ham*_*imp 13 powershell split powershell-2.0

我在配置文件(sample.cfg)中有以下内容,

Time_Zone_Variance(Mins):300
Alert_Interval(Mins):2
Server:10.0.0.9
Port:1840
Run Code Online (Sandbox Code Playgroud)

我试图在PowerShell中:使用后存储每个值split.但我无法产生需要的输出.

有人能告诉我如何使用PowerShell split来解决上述问题吗?

Dav*_*tin 11

您可以使用读取文件的内容Get-Content,然后管理每一行ForEach-Object,然后在每一行上使用split命令,获取数组中的第二项,如下所示:

$filename = "sample.cfg"

Get-Content $filename | ForEach-Object {
    $_.split(":")[1]
}
Run Code Online (Sandbox Code Playgroud)

产量

300
2
10.0.0.9
1840
Run Code Online (Sandbox Code Playgroud)

更新

我更喜欢@AnsgarWiechers的方法,但是如果你真的需要特定的命名值,你可以创建一个哈希表并用值替换名称:

$configValues = @{
    hour    = "Time_Zone_Variance(Mins)"
    min     = "Alert_Interval(Mins)"
    server  = "Server"
    port    = "Port"
}

Get-Content $filename | ForEach-Object {

    # Courtesy of Ansgar Wiechers
    $key, $value = $_ -split ':', 2

    foreach($configValuesKey in $($configValues.keys)) {
        if ($configValues[$configValuesKey] -eq $key)
        {
            $configValues[$configValuesKey] = $value
        }
    }
}

write-host "`nAll Values:"
$configValues
write-host "`nIndividual value:"
$configValues.port
Run Code Online (Sandbox Code Playgroud)

产量

All Values:

Name                           Value                                                                                             
----                           -----                                                                                             
port                           1840                                                                                              
min                            2                                                                                                 
server                         10.0.0.9                                                                                          
hour                           300                                                                                               

Individual value:
1840
Run Code Online (Sandbox Code Playgroud)


js2*_*010 10

这个怎么样?

function cut {
  param(
    [Parameter(ValueFromPipeline=$True)] [string]$inputobject,
    [string]$delimiter='\s+',
    [string[]]$field
  )

  process {
    if ($field -eq $null) { $inputobject -split $delimiter } else {
      ($inputobject -split $delimiter)[$field] }
  }
}


PS C:\> 'hi:there' | cut -f 0 -d :
hi

PS C:\> 'hi:there' | cut -f 1 -d :
there

PS C:\> 'hi:there' | cut -f 0,1 -d :
hi
there

PS C:\> 'hi:::there' | cut -f 0 -d :+
hi

PS C:\> 'hi   there' | cut
hi
there
Run Code Online (Sandbox Code Playgroud)


Ans*_*ers 6

我想您不想只是拆分行,而是实际创建键/值对。这可以像这样实现:

$config = @{}
Get-Content 'C:\path\to\sample.cfg' | % {
  $key, $value = $_ -split ':', 2
  $config[$key] = $value
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用ConvertFrom-StringDatacmdlet:

Get-Content 'C:\path\to\sample.cfg' | % {
  ConvertFrom-StringData ($_ -replace ':','=')
}
Run Code Online (Sandbox Code Playgroud)

-replace操作是必要的,因为ConvertFrom-StringData期望 key 和 value 以=. 如果您可以将配置文件中的分隔符从 更改:=,则ConvertFrom-StringData $_无需替换即可使用。


Ósc*_*ñiz 5

对于更简洁的语法,这也可以解决问题:

((Get-Content "your-file.txt") -Split ":")[1]
Run Code Online (Sandbox Code Playgroud)

所以使用该-Split方法的技巧是让一个String对象返回Get-Contentcat实际上也可以使用别名),然后从结果String[]对象中您可以使用括号提取第nth项。

注意:-Split不带括号使用Get-Content将不起作用,因为-Split它不是该命令的参数名称... ???