Ken J
Ken J

Reputation: 4562

Powershell Filter Tab-Delimited Text File

I've got a tab-delimited file that I'm running thru a foreach loop to match values.

foreach($mvalue in $mvalues) {
$vName = $mvalue.Name
$filter = "`"" + $vName + "/``t`""
$mMatch = gc d:\test.txt | select-string $filter
Write-Output $vName
Write-Output $filter
Write-Output $mMatch }

$mMatch is not outputting even though $filter is correct and I can do a test in the console with $filter's value and it will give me results. It might be important to mention that not all lines match the value that I'm searching for; but there are some that do and for those I would like it to output the value. I'm running 2.0 on Windows 2003.

Upvotes: 0

Views: 4883

Answers (4)

Shaounak Nasikkar
Shaounak Nasikkar

Reputation: 314

I recently had a similar requirement where I wanted to search list of values in a field from a file, like in SQL we use the IN clause

select * from table where column in (List of values)

Achieved using the following -

$lstExpenseID = @("VAL1"
 ,"VAL2"
 ,"VAL3")

Get-ChildItem -Filter "*.txt" -Path .\ |
ForEach-Object {
  Import-Csv -Delimiter `t -Path $_.FullName | Where-Object -Property "Cust id" -In -Value $lstExpenseID
}

Upvotes: 1

Ken J
Ken J

Reputation: 4562

It's always the little things:

I removed the parenthesis from around my variable:

$filter = $vName + "/\t"
$mMatch = gc d:\test.txt | select-string -pattern $filter

Upvotes: 0

Shay Levy
Shay Levy

Reputation: 126722

You can convert it to csv format and give columns a meaningful name:

Get-Content TabDlimited.txt | 
ConvertFrom-Csv -Header col1,col2,col3 -Delimiter "`t"  |
Where-Object {$_.col1 -match 'whatever'}

Upvotes: 1

JPBlanc
JPBlanc

Reputation: 72610

try to write you filter like this (\t is for tab in regular expression) :

$filter = "`"$vName/\t`""

Upvotes: 0

Related Questions