kirill_l
kirill_l

Reputation: 645

Using string split with tab characters in PowerShell 1.0

I'm processing tab-separated text file (ANSI) in PowerShell 1.0, and for some reason I can't split text in the file into multiple fields using the split function. The code below always returns 1 although there are five values separated by tab in each line of the file.

$f = get-content ‘users.txt’
foreach ($line in $f)
{
   $fields = $line.split('\t')
   $fields.count | Out-Host
}

I tested split with other separators like pipe, comma and it worked without problems.

Upvotes: 19

Views: 59488

Answers (2)

user3177926
user3177926

Reputation: 11

(Get-Content -LiteralPath C:\temp\Users.txt) | ForEach-Object {$_.Split("`t")} | Set-Content -Path C:\temp\Results.txt

Upvotes: 1

Tom H
Tom H

Reputation: 47444

You're using the wrong escape character for the tab. Try this instead:

$f = Get-Content "Users.txt"

foreach ($line in $f) {
    $fields = $line.Split("`t")
    $fields.Count | Out-Host
}

Upvotes: 29

Related Questions