Reputation: 5524
Developed this statement on my primary workstation where it (correctly) outputs a delimited textfile:
type output.tmp | -match /r /v "^-[-|]*-.$" > output.csv
Now, working on my laptop (same win8.1) where supposedly all the same PS modules and snapins are loaded, it tosses an error:
-match : The term '-match' is not recognized as the name of a cmdlet,
Yet:
"Software" –match "soft"
works.
1) Why?
2) Is there a PS commandlet I should invoke to be able to get a more verbose/helpful error output?
thx
Upvotes: 0
Views: 307
Reputation: 109130
-match
is an operator on two arguments (one placed before one after the -match
).
But at the beginning of each pipeline segment you need a command (including cmdlets)1.
There are two approaches:
Wrap the -match
into a cmdlet like foreach-object
(or its %
alias):
... | %{ $_ -match $regex } | ...
remembering that -match
returns a boolean, not the matched text.
Use Select-String
which is a cmdlet explicitly included for searching text. This does return the matched text (along with some other information), and can read a file itself:
Select-String -Path $inputFile $regex
1 Strictly speaking: except the first, which can be any expression.
Upvotes: 2
Reputation: 7638
get-Help Set-PSDebug
You simply need to add the following:
type output.tmp | ? { $_ -match /r /v "^-[-|]*-.$" } > output.csv
Or the more powershell-y way:
Get-Content -Path:"Output.Tmp" | Where { $_ -match "^-[-|]*-.$" } | Out-File -FilePath:"output.csv"
Upvotes: 0
Reputation: 32571
The reason for the error is that match
is a comparison operator, not a cmdlet:
Comparison operators let you specify conditions for comparing values and finding values that match specified patterns. To use a comparison operator, specify the values that you want to compare together with an operator that separates these values.
Also:
The match operators (-Match and -NotMatch) find elements that match or do not match a specified pattern using regular expressions.
The syntax is:
<string[]> -Match <regular-expression> <string[]> -NotMatch <regular-expression>
The following examples show some uses of the -Match operator:
PS C:\> "Windows", "PowerShell" -Match ".shell" PowerShell PS C:\> (Get-Command Get-Member -Syntax) -Match "-view" True PS C:\> (Get-Command Get-Member -Syntax) -NotMatch "-path" True PS C:\> (Get-Content Servers.txt) -Match "^Server\d\d" Server01 Server02
The match operators search only in strings. They cannot search in arrays of integers or other objects.
So, the correct syntax is:
@(type output.tmp) -match "^-[-|]*-.$" > output.csv
Note: Just as @mjolinor suggested, the @
prefix forces the (type output.tmp)
into an array, just in case that the input file contains only one line.
Upvotes: 1