Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
Powershell 查找图案之间的线条,并将第一行附加到线条_Powershell_Text Parsing - Fatal编程技术网

Powershell 查找图案之间的线条,并将第一行附加到线条

Powershell 查找图案之间的线条,并将第一行附加到线条,powershell,text-parsing,Powershell,Text Parsing,我正在尝试在Powershell中编写以下案例。我在bash终端上使用Sed完成了此练习,但在Powershell中编写时遇到了问题。任何帮助都将不胜感激。 (sed-r-e'/^N/h;/^[N-]/d;G;s/(.*)\N(.*)/\2\1/',文件格式不包含字符,环绕每行的第一个字母) 开始模式始终以开始(每个块仅1个实例),行之间以开始,结束模式始终为-- -------------- ABC123 有什么事吗 有什么事吗 有什么事吗 --------------以下基于管道的解决方案不

我正在尝试在Powershell中编写以下案例。我在bash终端上使用Sed完成了此练习,但在Powershell中编写时遇到了问题。任何帮助都将不胜感激。
sed-r-e'/^N/h;/^[N-]/d;G;s/(.*)\N(.*)/\2\1/'
,文件格式不包含
字符,环绕每行的第一个字母)

开始模式始终以
开始(每个块仅1个实例),行之间以
开始,结束模式始终为
--

--------------
ABC123
有什么事吗
有什么事吗
有什么事吗

--------------以下基于管道的解决方案不是快速的,但是在概念上是简单的

Get-Content file.txt | ForEach-Object {
  if ($_ -match '^-+$') { $newSect = $true }
  elseif ($newSect) { $firstSectionLine = $_; $newSect = $False }
  else { "{0}`t{1}" -f $_, $firstSectionLine }
}
((Get-Content -Raw file.txt) -split '(?m)^-+(?:\r?\n)?' -ne '').ForEach({
  $firstLine, $otherLines = $_ -split '\r?\n' -ne ''
  foreach ($otherLine in $otherLines) { "{0}`t{1}" -f $otherLine, $firstLine }
})
  • 它一行一行地读取和处理数据(手头的数据行反映在自动变量
    $\uu
    中)

  • 它使用带有
    -match
    运算符的正则表达式(
    ^-+
    )来标识节分隔符;如果找到,则设置标志
    $newSect
    ,表示下一行是节的第一个数据行

  • 如果命中第一个数据行,它将缓存在变量
    $firstSectionLine
    中,并重置
    $newSect
    标志

  • 根据定义,所有其他行都是第一个数据行要附加到的行,这是通过
    -f
    字符串格式运算符完成的,使用制表符(
    `t
    )作为分隔符


这里有一个更快的PSv4+解决方案
,它比PSv4+更复杂
,它将整个输入文件提前读取到内存中

Get-Content file.txt | ForEach-Object {
  if ($_ -match '^-+$') { $newSect = $true }
  elseif ($newSect) { $firstSectionLine = $_; $newSect = $False }
  else { "{0}`t{1}" -f $_, $firstSectionLine }
}
((Get-Content -Raw file.txt) -split '(?m)^-+(?:\r?\n)?' -ne '').ForEach({
  $firstLine, $otherLines = $_ -split '\r?\n' -ne ''
  foreach ($otherLine in $otherLines) { "{0}`t{1}" -f $otherLine, $firstLine }
})
  • Get Content-Raw
    以单个字符串的形式完整读取输入文件

  • 它使用
    -split
    操作符将输入文件拆分为多个部分,然后处理每个部分

  • Regex
    “(?m)^-+(?:\r?\n)?”
    匹配节分隔符行,可以选择后跟换行符

    • (?m)
      是多行选项,它使
      ^
      $
      分别匹配每行的开始和结束:
    • \r?\n
      匹配换行符,可以是CRLF(
      \r\n
      )或仅LF(
      \n
      )形式
    • (?:…)
      是一个非捕获组;将其设置为非捕获可防止其匹配的内容包含在
      -split
      返回的元素中
    • -ne'
      过滤掉生成的空元素
  • -split'\r?\n'
    将每个部分拆分为单独的行

如果性能仍然是一个问题,您可以使用
[IO.file]:ReadAllText($PWD/file.txt”)
加快文件的读取速度