What is an equivalent of *Nix 'cut' command in Powershell? What is an equivalent of *Nix 'cut' command in Powershell? powershell powershell

What is an equivalent of *Nix 'cut' command in Powershell?


You can read the contents of the file using Get-Content, then pipe each line through ForEach-Object, then use the split command on each line, taking the second item in the array as follows:

$filename = "sample.cfg"Get-Content $filename | ForEach-Object {    $_.split(":")[1]}

Output

300210.0.0.91840

Update

I prefer the approach by @AnsgarWiechers, but if you really need specifically named values you could create a hashtable and replace the name with the value:

$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:"$configValueswrite-host "`nIndividual value:"$configValues.port

Output

All Values:Name                           Value                                                                                             ----                           -----                                                                                             port                           1840                                                                                              min                            2                                                                                                 server                         10.0.0.9                                                                                          hour                           300                                                                                               Individual value:1840


How's this?

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 :hiPS C:\> 'hi:there' | cut -f 1 -d :therePS C:\> 'hi:there' | cut -f 0,1 -d :hitherePS C:\> 'hi:::there' | cut -f 0 -d :+hiPS C:\> 'hi   there' | cuthithere


I suppose you don't want to just split the lines, but actually create key/value pairs. That could be achieved like this:

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

You could also use the ConvertFrom-StringData cmdlet:

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

The -replace operation is necessary, because ConvertFrom-StringData expects key and value to be separated by =. If you could change the delimiter in the config file from : to =, you could use ConvertFrom-StringData $_ without replacement.