User Profile
Dalbir3
Copper Contributor
Joined 11 months ago
User Widgets
Recent Discussions
Re: split a text file using a string as delimiter
filigrana YouTube Udemy.com Microsoft learn google a few things if you want to invest into it pluralsight.com cbt nuggets amazon books essentially learn 1-5 commands then mix and match them, there tons of scripts on git hub Use visual studio code, powershell ise I would take what you have there and add more scope to it to learn more on the powershell side like log the output4.5KViews1like0CommentsRe: split a text file using a string as delimiter
The script creates new files in the same directory as the original file, naming them with a base name followed by a sequence number and the .txt extension (e.g., splitFile_1.txt, splitFile_2.txt, etc.). powershell Copy code # Define your parameters $filePath = "C:\path\to\your\file.txt" # Path to your huge text file $delimiter = "string" # Your delimiter $baseOutputPath = "C:\path\to\output\splitFile_" # Base path and filename for output files # Initialize variables $fileCounter = 1 $currentContent = @() # Read the file line by line Get-Content -Path $filePath | ForEach-Object { if ($_ -match $delimiter -and $currentContent.Count -gt 0) { # Output the current content to a file $currentContent | Out-File -FilePath ($baseOutputPath + $fileCounter + ".txt") # Increment the file counter and reset the current content $fileCounter++ $currentContent = @() } $currentContent += $_ } # Don't forget to output the last chunk if it exists if ($currentContent.Count -gt 0) { $currentContent | Out-File -FilePath ($baseOutputPath + $fileCounter + ".txt") } Here's how to use this script: Replace $filePath with the full path to your text file. Change $delimiter to the string you're using to split the files (it appears you're using "string" as your delimiter). Set $baseOutputPath to the directory and base filename where you want to save the split files. The script will append numbers to this base name to create the individual filenames. This script works by reading each line of the input file. Whenever it encounters the delimiter (indicating the start of a new section), it writes the accumulated lines to a new file and starts collecting lines afresh for the next file. Remember to adjust the file paths and delimiter according to your specific needs before running the script.4.5KViews1like3Comments
Groups
Recent Blog Articles
No content to show