Reputation: 105
I have no programming skills, yet a very specific task: I have to split one massive text-file into many, splitting them at a specific textmark (@). I decided to try Powershell script to accomplish this.
So far, that's what I have come up with:
InputFile = "c:\test.txt"
$Reader = New-Object System.IO.StreamReader($InputFile)
$a = 1
While (($Line = $Reader.ReadLine()) -ne $null) {
If ($Line -match "@") {
$OutputFile = "c:\splittedfile$a.txt"
$a++
}
Add-Content $OutputFile $Line
}
Now, what I want to achieve is not to simply base filenames on a loop. Instead, the second line of each new file is to act as filename, and I have no idea how to do this.
Edit @Jason Boyd: Specifying the structure of the file based on Jason's comment: It is basically a large template for a wiki, which is why the resulting textfiles need to have specific names (in the first example "buenos aires_audiencia_rpl". The number of lines is not constant, since "Comentario" may be pre-filled and have line breaks.
@
buenos aires_audiencia_rpl
======= Buenos Aires, Audiencia (RPL) =======
^Variantes | |
===== Comentario =====
==== Geografía ====
==== Historia institucional ====
==== Población y economía ====
==== Iglesia ====
==== Bibliografia ====
==== Citas textuales ====
@
caracas_audiencia_ven
Upvotes: 3
Views: 125
Reputation: 47792
You could read another line once your condition matches:
While (($Line = $Reader.ReadLine()) -ne $null) {
If ($Line -match "@") {
$OutputFile = $Reader.ReadLine()
Add-Content $OutputFile $Line
$Line = $OutputFile
$a++
}
Add-Content $OutputFile $Line
}
Upvotes: 3