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 split function. The code below always returns 1 although there are 5 values separated by tab in each line of the file.
我在powershell 1.0中处理制表符分隔的文本文件(ANSI),由于某种原因,我无法使用拆分功能将文件中的文本拆分为多个字段。下面的代码总是返回1,尽管在文件的每一行中有5个值由制表符分隔。
$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 w/o problems.
我测试了拆分与其他分隔符,如管道,逗号,它没有问题。
2 个解决方案
#1
20
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
}
#2
0
(Get-Content -LiteralPath C:\temp\Users.txt) | ForEach-Object {$_.Split("'t")} | Set-Content -Path C:\temp\Results.txt
#1
20
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
}
#2
0
(Get-Content -LiteralPath C:\temp\Users.txt) | ForEach-Object {$_.Split("'t")} | Set-Content -Path C:\temp\Results.txt