Reputation: 19
I am Having a huge text file consists of 5000 pages. I need to parse this single text file and create separate files. The page break needs to happen at every section containing text 'End of Statement'.
I am fairly new to Powershell script and hence would badly need help.
Kindly send me a script which I will guide me in a way that I can retrofit the code to work for me.
Upvotes: 0
Views: 682
Reputation: 72680
I'am going to avoid "what do you try so far etc ...". Generic question need generic answer :
Use Get-Content
cmdlet to load your text file, then pipe it to Select-String
to filter lines, then it's up to you !
Get-content "path_to_your_file" | Select-String -Pattern "your_filter"
You can also try a RegEx multilined to separate multiple lines (your pages).
Then you can use Set-Content
to xrite to another file.
Upvotes: 1
Reputation: 561
As you said you are new to PowerShell, you can go through below script to understand the logic and improve as per your understanding:
$data = Get-Content ".\Test.txt" #Reading file
$fileIndex = 1; #To create file name
for($itr = 0; $itr -le $data.Length; $itr++){
#if line is "End of Statement"
if($data[$itr] -eq "End of Statement"){
$fileIndex+=1;
continue;
}
#Check if file exists otherwise create one
if((Test-Path "$pwd\$fileIndex.txt") -eq $false){
New-Item "$pwd\$fileIndex.txt" -ItemType "File"
}
#Append text to the file
Add-Content "$pwd\$fileIndex.txt" $data[$itr]
}
The content of Test.txt
looks like below:
This is text in part1
This is text in part1
End of Statement
This is text in part2
This is text in part2
This is text in part2
End of Statement
This is text in part3
This is text in part3
End of Statement
This is text in part4
End of Statement
This is text in part5
End of Statement
This is text in part6
This is text in part6
It will create files with name 1.txt, 2.txt and so on. To execute please keep your .ps1 and .txt file in the same place.
Upvotes: 1