2016-07-30 135 views
-1

如果条件满足的列中名为“Total”的列中的所有值均为附加文件中的99.95,我想在第27列中的第27列至第30列中将“XRF” 。如果满足条件,则替换字

Example Source File

$InFolder = "C:\sif\" 
$OutFolder = "C:\Edited\" 
$files = Get-ChildItem $InFolder -Recurse -Include *.sif 
foreach ($file in $files) { 
    $OutFile = $OutFolder + $file.BaseName + "_FeC.sif" 
    $OutFile 
    $Lines = Get-Content $file 
    $Fe_C = "Y" 
    foreach ($Line in $Lines) { 
    while ($Fe_C -ne "N") { 
     if ($Line.ReadCount -ge 8) { 
     if (($line.Split(" ")) -eq "99.95") { 
      $Fe_C = "Y" 
     } else { 
      $Fe_C = "N" 
     } 
     } 
    } 
    } 
} 
+0

请张贴代码和数据作为*文本*而不是图像 –

+1

好吧,你有什么尝试?这个问题目前只是要求我们为您编写代码,而StackOverflow不是代码编写资源。 – JGreenwell

+0

对不起。这是我的脚本。它在检查最后一列的99.95时停止。我不知道如何使用split得到最后一列。 –

回答

1

您需要检查,如果没有数据线具有在数据列15比99.95其他值,并且如果是这样用“CALC取代在第5行“XRF”的第一次出现”。

为此替换此:

$Lines = Get-Content $file 
$Fe_C = "Y" 
foreach ($Line in $Lines) { 
    ... 
} 

与此:

$Lines = Get-Content $file 
$different = [bool]($Lines | 
      Select-Object -Skip 7 | 
      Where-Object { $_ } | 
      Where-Object { ($_ -split '\s+')[15] -ne '99.95' }) 
if (-not $different) { 
    $Lines[4] = $Lines[4] -replace 'XRF (.*)', 'CALC$1' 
} 
Set-Content -Path $file -Value $Lines 
0

使用正则表达式,并假设总计列是最后一个:

if (($lines[7..($lines.count-1)] -notmatch '\s99\.95\s*$|^\s*$').count -eq 0) { 
    $lines[4] = ([regex]'XRF').replace($lines[4], 'CALC', 1) 
} 
+0

谢谢Ansgar和wOxxOm。这两种解决方案都能满足我的期望。我已经学会了使用bool和CALC。赞赏。问候。鱼鳔 –