PowerShell5将index [0]设置为字符串的第一个实例,将index [1]设置为第二个实例,依此类推,直到在6800行Windows10批处理文件中完成

时间:2019-02-15 15:46:57

标签: powershell powershell-v5.0

Powershell5脚本阻塞了Windows 10批处理文件中的特殊字符,还是我的代码从根本上错了?

解析6800行Windows 10批处理文件,找到字符串{LINE2 1-9999},并将{1-9999}替换为代码所在的行号,重新编写批处理文件。 {LINE2 1-9999}有54个实例。如果我解析整个批处理,则会输出前54行,但都不包含字符串。

$lines = sls "LINE2" $env:windir\_61.bat | Select-Object -ExpandProperty LineNumber
gc $env:windir\_61.bat -OutVariable Content
$output = for ($index = 0; $index -lt $lines.count; $index++) {
$Content[$index] -replace "LINE2.*", "LINE2 $($lines[$index])"
} 
$output | sc "$env:TEMP\somebadhat.bat"

如果_61.bat是:

TITLE %TIME%   NO "%zmyapps1%\*.*" ARCHIVE ATTRIBUTE   LINE2 1243 
TITLE %TIME%   DOC/SET YQJ8   LINE2 1887 
SET ztitle=%TIME%: WINFOLD   LINE2 2557 
TITLE %TIME%   _*.* IN WINFOLD   LINE2 2597 
TITLE %TIME%   %%ZDATE1%% YQJ25   LINE2 3672 
TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 4922 

结果:

TITLE %TIME%   NO "%zmyapps1%\*.*" ARCHIVE ATTRIBUTE   LINE2 1
TITLE %TIME%   DOC/SET YQJ8   LINE2 2
SET ztitle=%TIME%: WINFOLD   LINE2 3
TITLE %TIME%   _*.* IN WINFOLD   LINE2 4
TITLE %TIME%   %%ZDATE1%% YQJ25   LINE2 5
TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 6 

如果_61.bat是:

TITLE %TIME%   NO "%zmyapps1%\*.*" ARCHIVE ATTRIBUTE   LINE2 1243
TITLE %TIME%   DOC/SET YQJ8   LINE2 1887
SET ztitle=%TIME%: WINFOLD   LINE2 2557
TITLE %TIME%   _*.* IN WINFOLD   LINE2 2597
TITLE %TIME%   %%ZDATE1%% YQJ25   LINE2 3672
TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 4922
:: Microsoft Windows [Version 10.0.17134.345] 64-BIT
@ECHO OFF 
TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 4923

结果:

TITLE %TIME%   NO "%zmyapps1%\*.*" ARCHIVE ATTRIBUTE   LINE2 1
TITLE %TIME%   DOC/SET YQJ8   LINE2 2
SET ztitle=%TIME%: WINFOLD   LINE2 3
TITLE %TIME%   _*.* IN WINFOLD   LINE2 4
TITLE %TIME%   %%ZDATE1%% YQJ25   LINE2 5
TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 6
:: Microsoft Windows [Version 10.0.17134.345] 64-BIT
@ECHO OFF

TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 4923

丢失。

@有30个实例。例如:

SET p= & title NetFix v1.0a by Giovanni Heward (g@utahjrs.com) complete!
%PROGRAMFILES%\7-Zip\7z.exe" u -t7z -x@"%WINDIR%\_exclude7z.txt"

是造成问题的原因吗?我用什么代替它?为什么,如果我解析整个文件,该文件具有该字符串的54个实例,它将输出54行,而其中的任何一行都不包含该字符串?我的代码有根本上的错误吗?

2 个答案:

答案 0 :(得分:1)

您正在执行的脚本只会解析输入脚本中包含“ LINE2 nnnn”标记的行数。因此,在上面的第二个输入示例中,在9行输入中,只有7行包含“ LINE2 nnnn”,因此仅处理了前7行。原因是这样的:

$lines = sls "LINE2" c:\temp\_61.bat | Select-Object -ExpandProperty LineNumber

此行包含所有包含匹配文本的行的数组,因此$lines是7个整数的数组。在后面的for循环中,您仅迭代了许多项:

$output = for ($index = 0; $index -lt $lines.count; $index++) {

您实际上并不需要$lines数组,只需使用内容上的循环索引来确定行号:

gc $env:windir\_61.bat -OutVariable Content 
$output = for ($index = 0; $index -lt $Content.count; $index++) {
$Content[$index] -replace "LINE2.*", "LINE2 $($index)"
} 
$output | sc "$env:TEMP\somebadhat.bat"

请注意,您可以通过删除临时变量,通过将所有数据推入管道来稍微清理一下脚本:

gc $env:windir\_61.bat | foreach -begin {$lc = 1} -process {
    $_ -replace "LINE2 \d*", "LINE2 $lc";
    $lc += 1
} | out-file "$env:TEMP\somebadhat.bat"

答案 1 :(得分:0)

只是可视化Select-String返回的内容以及如何替换基于索引的文件内容中的行号(zdan的方法效率更高)

## Q:\Test\2019\02\15\SO_54712715.ps1
$FilePath = '.\_61v2.bat'
$FileContent = Get-Content $FilePath
$FileMatches = Select-String -Path $FilePath -Pattern '(?<=LINE2 )\d+\s?$'

ForEach ($FM in $FileMatches){
   "{0}:{1} [{2}=>{0}]" -f $FM.LineNumber,$FM.Line,$FM.Matches.Value
   $FileContent[$FM.LineNumber-1] = $FM.Line.Replace($FM.Matches.Value,$FM.LineNumber)
}
$FileContent | Set-Content $FilePath

示例输出:

> Q:\Test\2019\02\15\SO_54712715.ps1
1:TITLE %TIME%   NO "%zmyapps1%\*.*" ARCHIVE ATTRIBUTE   LINE2 1243 [1243=>1]
2:TITLE %TIME%   DOC/SET YQJ8   LINE2 1887 [1887=>2]
3:SET ztitle=%TIME%: WINFOLD   LINE2 2557 [2557=>3]
4:TITLE %TIME%   _*.* IN WINFOLD   LINE2 2597 [2597=>4]
5:TITLE %TIME%   %%ZDATE1%% YQJ25   LINE2 3672 [3672=>5]
6:TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 4922 [4922=>6]
9:TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 4923 [4923=>9]

和生成的文件内容(上述bat的版本2)

> gc .\_61v2.bat
TITLE %TIME%   NO "%zmyapps1%\*.*" ARCHIVE ATTRIBUTE   LINE2 1
TITLE %TIME%   DOC/SET YQJ8   LINE2 2
SET ztitle=%TIME%: WINFOLD   LINE2 3
TITLE %TIME%   _*.* IN WINFOLD   LINE2 4
TITLE %TIME%   %%ZDATE1%% YQJ25   LINE2 5
TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 6
:: Microsoft Windows [Version 10.0.17134.345] 64-BIT
@ECHO OFF
TITLE %TIME%   FINISHED. PRESS ANY KEY TO SHUTDOWN ... LINE2 9