如果满足条件则替换单词

时间:2016-07-30 14:21:15

标签: powershell

如果附加文件中名为“Total”的列中的所有值都是99.95,我想在附加文件的第5行的第27到30行将“XRF”替换为“CALC”。

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"
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:1)

您需要检查数据列15中是否有数据行的值不是99.95,如果是,则将第5行中第一次出现的“XRF”替换为“CALC”。

要做到这一点,请将其替换为:

<body style="margin:0; padding:0; width:100% !important; font-family: verdana;">
<table width="100%" bgcolor="#F7F7F7" cellpadding="0" cellspacing="0" border="0" id="backgroundTable" align="center">
    <tr>
        <td>
        <table cellpadding="20" cellspacing="0" border="0" align="center">
            <tr>
                <td valign="top" align="center"><span style="color: rgb(44, 160, 209); font-size: 24px; font-family: verdana;">shift</span><span style="color: rgb(235, 42, 83); font-size: 24px; font-family: verdana; font-weight: bold;">Swap</span></td>
            </tr>
      </table>
    </td>
  </tr>
  <!-- This is where your content goes bro -->
  <tr>
    <td>
        <table width="600" bgcolor="#FFF" align="center" style="border-radius:8px;">
          <tr>
            <td style="padding: 35px;">
              <h3>
                Welcome <span style="text-decoration: none;"><%= @email %></span>!
              </h3>
              <div>
                <span style="display: block;">You can confirm your account email through the link below:</span>
                <br>
                <a href="<%= confirmation_url(@resource, confirmation_token: @token) %>" target ="_blank" style="display: block; color: orange; text-decoration: none; font-size: 150%;">Confirm your account</a>
                <br>
                <span style="display: block;">Or paste the following into the address bar: <%= confirmation_url(@resource, confirmation_token: @token) %></span>
                <h3 style="padding-top: 20px;">Thanks for signing up. We're looking forward to seeing you on the site!</h3>
              </div>
            </td>
          </tr>
        </table>
        </td>
    </tr>
    <tr>
        <td>
            <table width="600" align="center" cellpadding="50">
                <tr align="center"><td style="color: #2b2b2b";>Made by <a style="color: orange; text-decoration: none;" href="http://rafipatel.com">Rafi Patel</a> ©<%= Time.new.year %></td></tr>
            </table>
        </td>
    </tr>
</table>
</body>

用这个:

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

答案 1 :(得分:1)

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

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