Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex PowerShell获取内容并替换特定行中的对象_Regex_Powershell - Fatal编程技术网

Regex PowerShell获取内容并替换特定行中的对象

Regex PowerShell获取内容并替换特定行中的对象,regex,powershell,Regex,Powershell,我有一个包含以下内容的文本文件: Static Text MachineA MachineB MachineC Just Another Line 第一行有两个静态单词(静态文本),中间有空格。在这两个单词后面有0个或更多的计算机名,也用空格分隔 如果有0台计算机,但如果有1台或多台计算机,我需要找到一种方法将文本添加到第一行(第二行不变)。我需要用新的计算机名替换所有计算机名。因此,脚本应编辑文件以获得如下内容: Static Text MachineX MachineY Just Anot

我有一个包含以下内容的文本文件:

Static Text MachineA MachineB MachineC
Just Another Line
第一行有两个静态单词(静态文本),中间有空格。在这两个单词后面有0个或更多的计算机名,也用空格分隔

如果有0台计算机,但如果有1台或多台计算机,我需要找到一种方法将文本添加到第一行(第二行不变)。我需要用新的计算机名替换所有计算机名。因此,脚本应编辑文件以获得如下内容:

Static Text MachineX MachineY
Just Another Line
$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"

(Get-Content $OptionsFile) | % {
  if ($_.ReadCount -eq 1) {
    "Static Text $NewComputers"
  } else {
    $_
  }
} | Set-Content $OptionsFile
我已经用Regex查看了-replace函数,但不明白它为什么不起作用。这是我的脚本:

$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"

$content = Get-Content $OptionsFile
$content |
  ForEach-Object {  
    if ($_.ReadCount -eq 1) { 
      $_ -replace '\w+', $NewComputers
    } else { 
      $_ 
    }
  } | 
  Set-Content $OptionsFile

我希望有人能帮我解决这个问题。

检查一行是否以“静态文本”开头,后跟一系列单词字符,如果有匹配,请返回字符串:

Get-Content $OptionsFile | foreach {    
  if($_ -match '^Static Text\s+(\w+\s)+')
  {
      'Static Text MachineX MachineY'
  }
  else
  {
      $_
  }
}

检查一行是否以“静态文本”开头,后跟一系列单词字符,如果存在匹配项,则返回字符串:

Get-Content $OptionsFile | foreach {    
  if($_ -match '^Static Text\s+(\w+\s)+')
  {
      'Static Text MachineX MachineY'
  }
  else
  {
      $_
  }
}

如果
静态文本
没有出现在文件的其他位置,您可以简单地执行以下操作:

$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"

(Get-Content $OptionsFile) -replace '^(Static Text) .*', "`$1 $NewComputers" |
    Set-Content $OptionsFile

如果
静态文本
可以出现在其他地方,并且您只想替换第一行,则可以执行以下操作:

Static Text MachineX MachineY
Just Another Line
$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"

(Get-Content $OptionsFile) | % {
  if ($_.ReadCount -eq 1) {
    "Static Text $NewComputers"
  } else {
    $_
  }
} | Set-Content $OptionsFile
如果您只知道
静态文本
由第一行中的两个单词组成,但不知道它们确切是哪个单词,那么类似的方法应该可以工作:

$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"

(Get-Content $OptionsFile) | % {
  if ($_.ReadCount -eq 1) {
    $_ -replace '^(\w+ \w+) .*', "`$1 $NewComputers"
  } else {
    $_
  }
} | Set-Content $OptionsFile

如果
静态文本
没有出现在文件的其他位置,您可以简单地执行以下操作:

$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"

(Get-Content $OptionsFile) -replace '^(Static Text) .*', "`$1 $NewComputers" |
    Set-Content $OptionsFile

如果
静态文本
可以出现在其他地方,并且您只想替换第一行,则可以执行以下操作:

Static Text MachineX MachineY
Just Another Line
$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"

(Get-Content $OptionsFile) | % {
  if ($_.ReadCount -eq 1) {
    "Static Text $NewComputers"
  } else {
    $_
  }
} | Set-Content $OptionsFile
如果您只知道
静态文本
由第一行中的两个单词组成,但不知道它们确切是哪个单词,那么类似的方法应该可以工作:

$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"

(Get-Content $OptionsFile) | % {
  if ($_.ReadCount -eq 1) {
    $_ -replace '^(\w+ \w+) .*', "`$1 $NewComputers"
  } else {
    $_
  }
} | Set-Content $OptionsFile

很好!我现在换了整条线,笨蛋,我以前没想到这一点。非常感谢!:)很好!我现在换了整条线,笨蛋,我以前没想到这一点。非常感谢!:)