Forum Discussion
filigrana
Mar 31, 2024Copper Contributor
split a text file using a string as delimiter
greetings to the forum... i have a (huge) text file made like this ____________________________________ string blahblahblah(1) blahblahblah(2) string blahblahblah(3) blahblahblah(4) stri...
- Mar 31, 2024The 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.
Dalbir3
Mar 31, 2024Copper Contributor
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.
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.
- filigranaMar 31, 2024Copper ContributorIT WORKS!!!!!!!!!!!!
you are a great!!!!!!!
thank you!!!