【问题标题】:Find/replace string with carriage return and line feed in PowerShell在 PowerShell 中使用回车和换行符查找/替换字符串
【发布时间】:2013-02-23 09:40:56
【问题描述】:

有没有办法让 PowerShell 找到并用回车和换行符替换字符串(例如 ~}}|{{E)(\r\nE强>)?

例如:

$filenames = @("E:\blergfest.csv")
foreach ($file in $filenames) {
    $outfile = "$file" + ".out"

    Get-Content $file | Foreach-object {
        $_ -replace '\~}}|{{E', '\r\nE' `
            -replace '\|\r\n', '\r\n'
    } | Set-Content $outfile
}

【问题讨论】:

    标签: powershell control-characters


    【解决方案1】:

    要创建包含带有回车和换行符的控制字符的字符串,您可以使用双引号并使用反引号字符 `,这是 powershell 转义码。像这样:

    "`r`nE"
    

    【讨论】:

      【解决方案2】:

      这个怎么样:

      $filenames = @("E:\blergfest.csv")
      foreach ($file in $filenames) {
          $outfile = "$file" + ".out"
      
          Get-Content $file | Foreach-object {
              $_ -replace '\~}}|{{E', "`r`nE" `
                  -replace '\|\r\n', "`r`n"
          } | Set-Content $outfile
      }
      

      【讨论】:

      • 要求您在替换文本中使用反斜杠 ` 而不是反斜杠 \,如下所示:(?)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2017-06-15
      • 2021-12-19
      • 2019-03-05
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多