简体   繁体   English

PowerShell中获取多行代码

[英]Getting multiple lines of code in PowerShell

I want to get the multiple lines from a text file by using a pattern with the character at the beginning of the string to the character at the end of the string,and have an output that will be copied as multiple lines also.我想通过使用字符串开头的字符到字符串末尾的字符的模式从文本文件中获取多行,并且有一个 output 也将被复制为多行。

<#
  get
  these
  lines
#>

I have tried using regex "<#(.+)#>", it gets the lines but it copies as one string only.我试过使用正则表达式“<#(.+)#>”,它获取了行但它只复制为一个字符串。

Output: <# get these lines #> Output:<#获取这些行#>

FileLines3.TXT:文件行 3.TXT:

Random info1
<#
  get1
  these1
  lines1
#>
Random info2
<#
  get2
  these2
  lines2
#>
Random info3

Code:代码:

$FileContent = Get-Content -Path "$PSScriptRoot\FileLines3.TXT" -Raw
$Lines = while($FileContent -match '(?ms)^\s*<#\s*$(?<Block>.*?)#>\s*$(?<After>.*)') {
    $Matches.Block.Split([Environment]::NewLine)
    $FileContent = $Matches.After
}
$Lines | Where-Object {$_}

Results:结果:

  get1
  these1
  lines1
  get2
  these2
  lines2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM