split a text file into two or more parts

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
drewdavis
Posts: 1
Joined: Fri Aug 02, 2024 7:48 am

split a text file into two or more parts

Post by drewdavis »

Hi everyone,
I want to split a text file into two or more parts, or delete certain lines. I have these huge text files, some of which are around 400k lines. I want to split the file into multiple parts to create two (or more) separate files of equal size, or just delete all lines from the 200k-400k rows. I just downloaded textpad, so I'm a complete newbie.
Thanks in advance!
User avatar
AmigoJack
Posts: 532
Joined: Sun Oct 30, 2016 4:28 pm
Location: グリーン ヒル ゾーン
Contact:

Re: split a text file into two or more parts

Post by AmigoJack »

drewdavis wrote: Fri Aug 02, 2024 7:59 amjust delete all lines from the 200k-400k rows
  1. Press Ctrl+G (or use the menu "Search > Go To...").
  2. Make sure the radiobutton "Line" is chosen, then enter "200000" as value, because that's the line (or "row") you want to go to. Press button "Go To".
  3. Press Shift+Ctrl+End, so you select all text (Shift) from that position to the end of the document (Ctrl+End).
  4. Press Del (or use the menu "Edit > Delete > Selection").
Knowing the essential key combinations which work for the entire operating system (not just in TextPad) like you should already know Ctrl+C and Ctrl+V will help you drastically more than relying on specific tasks in specific programs. You can always do these (incomplete list):
  • Ctrl+Home = start of document
  • Ctrl+End = end of document
  • Shift+Right/Left/Up/Down = extend current selection per character/line
  • Ctrl+Shift+Right/Left/Up/Down = extend current selection per word/paragraph
  • Ctrl+Right/Left/Up/Down = go to next word/paragraph (instead of just next character/line with arrow keys only)
  • Ctrl+A = select everything (whole document)
  • Ctrl+Z = undo last operation
  • Ctrl+F = (quickly) find
  • F3 = find next


To "split" one document in many I would use a very similar approach:
  1. Press Ctrl+G (or use the menu "Search > Go To...").
  2. Make sure the radiobutton "Line" is chosen, then enter "200000" as value, because that's the line (or "row") you want to go to. Press button "Go To".
  3. Press Shift+Ctrl+Home, so you select all text (Shift) from that position to the start of the document (Ctrl+Home).
  4. Press Ctrl+X (or use the menu "Edit > Cut") to move all those lines into the clipboard.
  5. Press Ctrl+N (or use the menu "File > New") to create a new document.
  6. Press Ctrl+V (or use the menu "Edit > Paste") to copy all lines from the clipboard into the new document (the clipboard remains filled).
  7. (optional) Press Ctrl+S (or use the menu "File > Save") to store the new document under a given filename.
  8. Press Ctrl+F4 (or use the menu "File > Close") to close the saved document. If you left out the previous step you're now being asked if you want to save your document to a file first.
  9. Repeat from step #1 until your original files becomes empty. Then it's up to you if you want to save the changes to that or not.
User avatar
bbadmin
Site Admin
Posts: 878
Joined: Mon Feb 17, 2003 8:54 pm
Contact:

Re: split a text file into two or more parts

Post by bbadmin »

There are various ways of splitting a file using PowerShell described here:

https://powershellfaqs.com/split-large- ... owershell/

https://stackoverflow.com/questions/100 ... powershell

Having said that, the script below runs mind-numbingly slowly, so AmigoJack's solution has much to recommend it!

Code: Select all

# Script to split a file by line count
# Usage: powershell.exe -file splitfile.ps1 BIGFILE 1000
# Based on example at https://powershellfaqs.com/split-large-text-files-with-powershell/

$sourceFile = $args[0]
$lineCount = $args[1]  # The number of lines each split file should contain
$counter = 1

Get-Content $sourceFile | ForEach-Object {
    $fileName = $sourceFile + $counter.ToString("000")
    Add-Content -Value $_ -Path $fileName

    if ((Get-Content -Path $fileName).Count -ge $lineCount) {
        $counter++
    }
}
Note that TextPad's GoTo command accepts a relative line number, so from the first line, you could repeatedly goto line +1000 with the Extend Selection option checked, use the Save As command to save the selected text, then press the Esc key to clear the selection.
Post Reply