在变量中替换CR和LF

时间:2020-07-01 13:36:14

标签: powershell

我有一堆文本文件,我试图在其中查找并计算两个字符字符串的出现次数。

找到后,我将值存储在变量中,然后将该值添加到数组中。

我的问题是我变量中的值包含CR和LF,无法删除。

我这样加载数据:

$arrayToSearch = Get-Content $myFile | Select-String -Pattern $mySearchString

foreach($lineToProcess in $arrayToSearch)
{
  $myVar = ($lineToProcess -split $leadingStringPattern) -split $trailingSringPattern

  #here I need $myVar to only contain AB and not any Cr and/or Lf
}

我尝试了这些不同的变体:

$myVar = $myVar -replace '(?:\s|\r|\n)',''

$myVar = $myVar.replace("\n","")

$myVar = $myVar.name.substring(0,2)

$myVar = $myVar -split "\n"

$myVar = $myVar -split "`r`n"

$myVar = $myVar -replace "`t|`n|`r",""

$newLine = [environment]::NewLine
$myVar = $myVar -replace $newLine,""

均无效。还有其他建议吗?

我得到了什么(CrLf =回车和换行):

CrLf

ABCrLf

CrLf

我想要什么:

AB

0 个答案:

没有答案
相关问题