我对powershell很陌生,喜欢学习基础知识,但我对这个问题从哪里开始有点迷失。
示例输入文件
文件名data.txt
RECSTART001 RECkjdsfgjk4897 0934578ldkfjgk recfgh lkjsef 3459086001
REC765 shop9994 product345 lkdhfgjh74654001 START897
REC84 SEPTEMBER9 kjh456097 765 84 RECfgh 234 67 123 dog
RECEND shop9994 0934578ldkfjgk 234 67 123 dog
RECSTART001 FREDkjdsfgjk4897 line 2 recfgh lkjsef 3459086001
REC765 shop9991 product666 lkdhfgjh74654001 END897
REC84 SEPTEMBER kjh456097 765 84 RECfgh 234 67 123 cat APRIL 1 2 888 9
REC84 SEPTEMBER kjh456097 765 84 RECfgh 234 67 123 fish APRIL food sd d g8 r2
RECEND shop9994 0934578ldkfjgk 234 67 123 dog
RECSTART001 RECkjdsfgjk4897 0934578ldkfjgk recfgh lkjsef 3459086001
REC001 shop9994 product345 lkdhfgjh74654001 START897
REC12 Vanilla9 dip456097 mint 84 RECfgh 234 67 555 box
REC765 break lolly097 bmw 84 RECfgh 234 bingo 555 cabbage 984756 djd 8
RECEND shop9994 0934578ldkfjgk 234 67 123 dog
所需的输出文件
文件名data.rec001.txt
RECSTART001 RECkjdsfgjk4897 0934578ldkfjgk recfgh lkjsef 3459086001
REC765 shop9994 product345 lkdhfgjh74654001 START897
REC84 SEPTEMBER9 kjh456097 765 84 RECfgh 234 67 123 dog
RECEND shop9994 0934578ldkfjgk 234 67 123 dog
文件名data.rec002.txt
RECSTART001 FREDkjdsfgjk4897 line 2 recfgh lkjsef 3459086001
REC765 shop9991 product666 lkdhfgjh74654001 END897
REC84 SEPTEMBER kjh456097 765 84 RECfgh 234 67 123 cat APRIL 1 2 888 9
REC84 SEPTEMBER kjh456097 765 84 RECfgh 234 67 123 fish APRIL food sd d g8 r2
RECEND shop9994 0934578ldkfjgk 234 67 123 dog
文件名data.rec003.txt
RECSTART001 RECkjdsfgjk4897 0934578ldkfjgk recfgh lkjsef 3459086001
REC001 shop9994 product345 lkdhfgjh74654001 START897
REC12 Vanilla9 dip456097 mint 84 RECfgh 234 67 555 box
REC765 break lolly097 bmw 84 RECfgh 234 bingo 555 cabbage 984756 djd 8
RECEND shop9994 0934578ldkfjgk 234 67 123 dog
PowerShell可以实现吗?如果是的话,我从哪里开始?
提前致谢
答案 0 :(得分:0)
如果您想在每次以RECEND
开头的行之后更改为新文件:
param (
$FilePath = 'C:\Temp\data.txt',
$OutputFolder = 'C:\Temp'
)
$Counter = 1
Get-Content $FilePath | ForEach-Object -Process {
$FileName = "$OutputFolder\data.rec{0:D3}.txt" -f $Counter
$_ | Add-Content -Path $FileName
if ( $_ -like "RECEND*" )
{
$Counter ++
}
}
如果您希望每次以RECSTART
开头的行开始新文件:
param (
$FilePath = 'C:\Temp\data.txt',
$OutputFolder = 'C:\Temp'
)
$Counter = 0
Get-Content $FilePath | ForEach-Object -Process {
if ( $_ -like "RECSTART*" )
{
$Counter ++
}
$FileName = "$OutputFolder\data.rec{0:D3}.txt" -f $Counter
$_ | Add-Content -Path $FileName
}