Powershell文件拆分
我想用Powershell将一个包含许多块的大文件拆分为几个文件,每个文件都有一个块,每个文件都会从该块中的某个单词中获取一个文件名。每个块包括:开始页眉、文件中的文本和结束页脚 文件示例:Powershell文件拆分,powershell,Powershell,我想用Powershell将一个包含许多块的大文件拆分为几个文件,每个文件都有一个块,每个文件都会从该块中的某个单词中获取一个文件名。每个块包括:开始页眉、文件中的文本和结束页脚 文件示例: --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
--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--
拆分后的结果:
--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--
--start--
Text in a file that contains word "bbb"
--end--
--start--
Text in a file that contains word "ccc"
--end--
文件:bbb.txt
,内容:
--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--
文件:ccc.txt
,内容:
--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--
我把这个放在这里,因为它对于评论部分来说太长了: 关于此主题,有一个完整的系列文章,其中有一些示例:
Find-Module -Name '*splitter*'
Version Name Repository Description
------- ---- ---------- -----------
1.3 FileSplitter PSGallery provides PowerShell commands to split a file in multiple parts, ...
使用方法如下:
switch -file large.txt {
'--start--' { $header = $_; continue }
'--end--' { $footer = $_; , $header + $content + $footer > $outFile; continue }
default { $content = $_; $outFile = ($_ -split '"')[1] + '.txt' }
}
注意:以上假设页眉和页脚行之间正好有一个内容行;处理多个内容行需要做更多的工作。您有什么问题吗?因此,这不是一个脚本编写服务。