我有一个文件DJ.bat文件。我正在使用powershell读取文件的内容。
DJENGINE -l "Y:\st\Retail\FTP\2022-03-23\TMSfuture_cost_2554_20220323065246.tf.err" -sc "File=Y:\st\Retail\FTP\2022-03-23\TMSfuture_cost_2554_20220323065246.dat" -tc "Server=ABC.stg.sql.ccaintranet.com;Database=ABCTMS_FC;Table=dbo.FC_Load" "\\ccaintranet.com\dfs-dc-01\Data\Retail\Actian11\MapDesigner\fc.tf.xml"
DJENGINE -l "Y:\st\Retail\FTP\2022-03-23\TMSfuture_cost_5168_20220323074029.tf.err" -sc "File=Y:\st\Retail\FTP\2022-03-23\TMSfuture_cost_5168_20220323074029.dat" -tc "Server=ABC.stg.sql.ccaintranet.com;Database=ABCTMS_FC;Table=dbo.FC_Load" "\\ccaintranet.com\dfs-dc-01\Data\Retail\Actian11\MapDesigner\fc.tf.xml"
DJENGINE -l "Y:\st\Retail\FTP\2022-03-23\TMSfuture_cost_13272_20220323070111.tf.err" -sc "File=Y:\st\Retail\FTP\2022-03-23\TMSfuture_cost_13272_20220323070111.dat" -tc "Server=ABC.stg.sql.ccaintranet.com;Database=ABCTMS_FC;Table=dbo.FC_Load" "\\ccaintranet.com\dfs-dc-01\Data\Retail\Actian11\MapDesigner\fc.tf.xml"我已经打印了上千行DJENGINE语句.我刚才提到了三个
我有一个数组$data,它有数千个数据。我刚才在这里提到了两个:
TMSfuture_cost_2554_20220323065246.dat
TMSfuture_cost_5168_20220323074029.dat我想要的结果是content of array是在content of file being read内部进行比较的。如果数组的内容与匹配,那么我需要从以下位置删除代码块:
" DJEngine.... to .... tf.xml"由于这些two arrays与file content匹配,所以我的预期输出是:
DJENGINE -l "Y:\st\Retail\FTP\2022-03-23\TMSfuture_cost_13272_20220323070111.tf.err" -sc "File=Y:\st\Retail\FTP\2022-03-23\TMSfuture_cost_13272_20220323070111.dat" -tc "Server=ABC.stg.sql.ccaintranet.com;Database=ABCTMS_FC;Table=dbo.FC_Load" "\\ccaintranet.com\dfs-dc-01\Data\Retail\Actian11\MapDesigner\fc.tf.xml"我试着用:
$pathOFDj='Y:\St\Retail\FTP\2022-03-23\DJ.bat'
foreach($d in $data){
foreach($line in Get-Content $pathOFDj) {
if($line -contains $d){
}
else{
$newLine+=$line
}
}
}
echo $newLine我提到的那块没有被移走。
发布于 2022-03-24 11:23:45
您当前的脚本有三个主要问题:
instead
-contains用于测试集合包含,而不是子字符串--您可能希望在字符串比较中使用-like或-match之类的东西--即使在特定的行中找到了$data中的一个字符串,在外部循环的下一次迭代中仍将复制/包含行,因为它将不包含剩余的substrings$data中的每一项,可以使脚本获得O(N*M)的边界时间复杂性,其中N是$data项的数量,M是文件中的行数。这意味着当您增加输入大小时,代码会明显变慢。通过以不同的方式构造代码,这可以得到一定程度的改进,通过以不同的方式构造数据,可以对进行大规模的改进。
我将向您展示如何准备$data数组并解析输入文件以获得更好的性能(当然也是正确的),而不是试图逐点解决这些问题。
这将包括两个步骤:
$data项组织成允许常量时间查找的数据结构,这可以尽快告诉我们某个特定字符串是否是集合的一部分。。
$pathOFDj='Y:\St\Retail\FTP\2022-03-23\DJ.bat'
# Read file names into array
$data = Get-Content path\to\listOfFileNames.txt
# Create a hashset - this will provide super-fast lookups
$fileNameSet = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::CurrentCultureIgnoreCase)
$data |ForEach-Object { [void]$fileNameSet.Add($_) }
# Now we can start parsing and filtering the file
Get-Content $pathOFDj |Where-Object {
# attempt to extract file name, then use the extracted file name to test if it's one of the relevant file names
-not($_ -match '-sc "File=[^"]+?\\([^\\"]+)"' -and $fileNameSet.Contains($Matches[1]))
} |Set-Content path\to\modified_dj.bat只有在成功提取文件名并发现该文件名包含在哈希集中时,-not($_ -match '-sc "File=[^"]+\\([^\\"]+)"' -and $fileNameSet.Contains($Matches[1]))语句才能计算为$false --否则,它将计算为$true,而Where-Object将按预期让行筛选通过。
https://stackoverflow.com/questions/71600620
复制相似问题