我希望 $match 将始终为空。根据帮助,Out-File 从不向管道发送任何内容。但如果这与您的问题有关,我会感到惊讶。
本着“任何帮助将不胜感激”的精神......
我在 Win7 和 WS08R2 上尝试了一个简单的实验,但无法重现您的结果(它按预期工作)。我还尝试创建使用不同编码搜索的文件:Unicode、UTF8 和 ASCII。一切正常。我什至告诉 Select-String 使用错误的编码:
get-content mytestfile.ascii | select-string error -Encoding Unicode
它仍然有效。我使用十六进制查看器来验证 mytestfile.ascii、mytestfile.unicode 和 mytestfile.utf8 实际上都以不同的方式编码。我还尝试了不同的行尾(Linux 风格与 Windows 风格),并且奏效了。
我的猜测是你对文件、系统等有一些独特的东西,必须通过一些探测来发现。考虑到这一点...
我建议您使用十六进制查看器查看其中一个日志文件(从两个系统中尝试),看看您的 Win7 系统和 WS08R2 系统之间是否有不同之处。在 UTF8 文件的开头,您将看到 EF BB BF,然后是日志中每个字符的一个字节。在 Unicode 文件的开头,您会看到 FF FE,然后是每个字符的两个字节。(如果文本是英文,那么每隔一个字符将是 0)。对于 ASCII 文件,开头没有特殊值,每个字母有一个字节。Linux 行结尾是单个 0A(换行),Windows 行结尾是 0D 0A(回车,换行)。除了上面提到的开头的特殊字符和行尾,您会寻找文本与您在记事本中查看的字符集完全相同。(顺便提一句,如果行尾是 Windows 样式,则在记事本中查看它只会看起来“正确”)。如果您看到任何多余的字符,那么这可能是问题的根源
如果这没有显示出任何意外,那么我建议从 Powershell 命令行进行一系列实验。以下过程列出了很多步骤,但它试图涵盖很多意外情况。你不会执行每一步。(但是,在开始时进行了一些修改 - 考虑到“错误”出现在您用于测试的日志文件中的位置 - 您可以复制并粘贴整个脚本,然后一次解释整组结果)
# 0. Get some text to experiment with
# If "error" appears on line 200 and line 493 (where first line in file is Line 1), then:
$testTxt= cat mylogfile | select-object -skip 195 -first 300
# or better still:
$testTxt= cat mylogfile | select-object -skip 195 -first 10
$testTxt2= cat mylogfile | select-object -skip 488 -first 10
$testTxt += $testTxt
# 1. Figure out where in testTxt "error" appears
$testTxt[3..5] # if my math is right "error" will appear as middle line
# 2. If #1 doesn't work on both systems, then on failing system(s):
$testTxt[4] | select-string error
# 2.5 If #2 fails then you need to look at every character
# of $testTxt
[char[]]$testTxt
# or even look at every byte value in hex:
[byte[]][char[]]$textTxt | % { $_.tostring("X")}
# 3. If #2 worked, then:
$testTxt | select-string error
# 4. If #3 failed, then:
$testTxt[3..5] | select-string error
# 5. If #4 failed, then:
$testTxt[3..4] | select-string error
$testTxt[4..5] | select-string error
# 6. If either of #5 failed, then carefully examine those lines of text:
"line 3"
[char[]]$testTxt[3]
"line 4"
[char[]]$testTxt[4]
"line 5"
[char[]]$testTxt[5]
# 7. If #3 worked then:
$testTxt2 = cat myLogFile
$testTxt2 | select-string error
# 8. If #7 worked, then you have a solution
# Replace:
get-content $file | Select-String $p | Out-File $matchfile
# With (on Powershell v3 this may perform worse than the above, assuming above worked)
$logText = get-content $file
$logText | Select-String $p | Out-File $matchfile
# 9. If #8 failed, then the situation is somewhat awkward.
# On one hand you have twenty lines working, but on the other the file as a whole
# doesn't work. You could go back to step #0 and start with a larger amount of text,
# say 40 lines, and see if it works. If it does you could try 80, 160, etc. until it
# fails. But this case seems pretty unlikely.
# 10. In general when a step fails you want to try something "smaller". And when steps
# succeed you want to try something "bigger", where "bigger" means more like the
# solution you want.