【问题标题】:Powershell file splittingPowershell 文件拆分
【发布时间】:2019-11-28 19:28:23
【问题描述】:

我想使用包含许多块的 Powershell 将一个大文件拆分为多个文件,其中每个文件有 1 个块,每个文件将从位于该块中的某个单词中获取文件名。每个块包括:开始页眉、文件中的文本和结束页脚。

文件示例:

--start--
Text in a file that contains word "aaa"
--end--
--start--
Text in a file that contains word "bbb"
--end--
--start--
Text in a file that contains word "ccc"
--end--

拆分后的结果:

文件:aaa.txt 内容:

--start--
Text in a file that contains word "aaa"
--end--

文件:bbb.txt 内容:

--start--
Text in a file that contains word "bbb"
--end--

文件:ccc.txt 内容:

--start--
Text in a file that contains word "ccc"
--end--

【问题讨论】:

  • 您有问题吗? SO 不是脚本编写服务。

标签: powershell


【解决方案1】:

我把它放在这里,因为它对于评论部分来说太长了:

关于这个主题有一个完整的文章系列,带有示例:

这些文章指向 MS powershellgallery.com 中的一个模块,人们也可以利用。

 Find-Module -Name '*splitter*'

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
1.3                  FileSplitter                        PSGallery            provides PowerShell commands to split a file in multiple parts, ...

【讨论】:

    【解决方案2】:

    如下使用switch -file

    switch -file large.txt {
      '--start--' { $header = $_; continue }
      '--end--'   { $footer = $_; , $header + $content + $footer > $outFile; continue }
      default     { $content = $_; $outFile = ($_ -split '"')[1] + '.txt' }
    }
    

    注意:以上假设在页眉和页脚行之间正好有 1 行内容;需要更多的工作来处理多个内容行。

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      相关资源
      最近更新 更多