Results 1 to 12 of 12
  1. #1
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Powershell - Split a Text File - Output With Delimiter As File Name

    Folks,

    good day to all and every one.

    Powershell newbie, as of recent.

    I wanted to split a text file into smaller ones.

    The file names of each text file are between placeholders

    XXX File Name 1 ZZZ

    some content here

    XXX Filename2 ZZZ

    some content here

    I have been grappling with this below

    http://superuser.com/questions/46636...ple-text-files


    Code:
    # Split a Text File  - By A Delimiter
    
    
    $Path = "C:\Users\PBL\Desktop\b"         # Folder Containing the Text file  - And where Files will be Split
    
    
    $InputFile = (Join-Path $Path "b.txt")
    
    
    $Reader = New-Object System.IO.StreamReader($InputFile)
    
    While (($Line = $Reader.ReadLine()) -ne $null) {
        If ($Line -match " [regex]'(XXX)*(ZZZ)' {                          # This is wrong
            $OutputFile = $matches[1] + ".txt"                               # This is wrong
        }
    
        Add-Content (Join-Path $Path $OutputFile) $Line
    }

    To summarize

    The filename of each text file is between

    XXX filename ZZZ - couldn't think of anything more imaginative

    I want to split 1 large text file every time it finds a delimiter which is the XXX filename ZZZ

    I hope I'm making more sense than last time - Ive been all over and just can't find anything to fix it

    thanks folks and RG,

    really appreciative of all the help

    pb

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,188
    Thanks
    47
    Thanked 983 Times in 913 Posts
    The issue is your match. $matches[1] = XXX and you want to match the file name. The brackets are a grouping modifier and the use of $matches[x] returns a specific group. Try this line:
    If ($Line -match "XXX(.*)ZZZ") {

    cheers, Paul

  3. #3
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    Hello Paul,

    thank you for your help

    Does below look right

    Code:
    While (($Line = $Reader.ReadLine()) -ne $null) {
        
        If ($Line -match "XXX(.*)ZZZ") {
    
                                   
            $OutputFile = $matches[1] = XXX  + ".txt"
        }
    
        Add-Content (Join-Path $Path $OutputFile) $Line
    }
    I ran it but no split

    I think we are nearly there - it looks a lot more logical

    cheers

    pb

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    PB,

    A slightly different approach...
    Code:
    $FNPattern = [RegEx] '(xxx)*(zzz)'
    
    $Path = "G:\Test\"         # Folder Containing the Text file  - And where Files will be Split
    
    $SourceData = Get-Content -Path "$($Path)SplitTest.txt"
    
    ForEach ($Line in $SourceData) {
    
      
      If ($Line -match $FNPattern) {
        $Part = $line.split("xxx")
        $FN = $Part[3].split("zzz")
        $CurrentFN = $Path + "$($FN[0].trim())" + ".txt"
      }
      Else {
      Add-Content -Path "$CurrentFN" -Value $Line
      }
    }
    Source File: SplitTest.txt
    Code:
    xxx FirstFile zzz
    FirstFile line 1
    FirstFile line 2
    FirstFile line 3
    FirstFile line 4
    FirstFile line 5
    FirstFile line 6
    xxx SecondFile zzz
    SecondFile line A
    SecondFile line B
    SecondFile line C
    SecondFile line D
    Results: FirstFile.txt
    Code:
    FirstFile line 1
    FirstFile line 2
    FirstFile line 3
    FirstFile line 4
    FirstFile line 5
    FirstFile line 6
    Results:SecondFile.txt
    Code:
    SecondFile line A
    SecondFile line B
    SecondFile line C
    SecondFile line D
    HTH
    Last edited by RetiredGeek; 2016-03-03 at 13:23.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    RG,

    thank you it split the file nicely.

    How Do I make the name of the file with the match


    XXX File Name 1 ZZZ >> File Name 1.txt
    XXX File Name 2 ZZZ >> File Name 2.txt

    etc

    $CurrentFN = $Path + $FNPattern + "$($FN[0].trim())"

    do I add the pattern match to it?

    thanks RG

    pb

  6. #6
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    PB,

    I don't understand the question as the code as currently written will handle any legal file name, including ones w/spaces, and add the .txt extension.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  7. #7
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    Hi RG,

    awww, i hope I'm not muddying things up again. Been a long day.

    When you have a spare moment, would you be kind enough to see if it splits this file

    File.txt

    I tried and for some reason it did not split it.

    it names it with the file name as demonstrated in your post - so please ignore previous post - got powershell mania today

    cheers

    pb

  8. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    PB,

    It works fine if you change the code items from xxx zzz to XXX ZZZ. I'm working on making it work w/either upper or lower case. HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  9. #9
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    PB,

    Ok here's a revision that works in all cases of file names & delimiters.
    Code:
    $FNPattern = [RegEx] '([xX]{3})*([zZ]{3})'
    
    $Path = "G:\Test\"  # Folder Containing the Text file 
                        # And where result Files will be placed
    
    $SourceData = Get-Content -Path "$($Path)SplitTest.txt"
    
    ForEach ($Line in $SourceData) {
    
      If ($Line -match $FNPattern) {
        $FN = $Line.Substring(3,($Line.Length-6))
        $CurrentFN = $Path + "$($FN.trim())" + ".txt"
      }
      Else {
      Add-Content -Path "$CurrentFN" -Value $Line
      }
    
    }  #End ForEach
    Test file:
    Code:
    XXX  File Name 1 ZZZ
    
    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae.
    
    
    XXX File Name 2  ZzZ
    
    Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? 
    
    XXx FileName 3  ZZZ
    Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?
    HTH
    Last edited by RetiredGeek; 2016-03-03 at 18:50.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  10. The Following User Says Thank You to RetiredGeek For This Useful Post:

    pb89 (2016-03-03)

  11. #10
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Cool

    RG,

    you are too awesome and too kind.

    I have tested it - worked like a charm -

    Its all your fault - you have been showing how power shell is pretty awesome - and now I have got the bug.

    VBA is good - but it gets messy having to convert text files to docx - to split - or mail merge split....

    Some times you just want to SPLIT A TEXT FILE - no thrills attached


    Thanks for the awesome - generous help

    Really indebted

    Have a great weekend now
    cheers

    pb

  12. #11
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    PB,

    Here's an improved version that simplifies the code for determining the file name and also adds two switches. One is present will strip out blank lines from the resulting files. The other will erase any files in the target directory that already exist, otherwise the new data would be appended.

    Code:
    Param (
    
      [Parameter(Mandatory=$false)]
        [Switch] $OldFileDelete,
      [Parameter(Mandatory=$false)]
        [Switch] $StripBlankLines
    )
    
    $FNPattern = [RegEx] '([xX]{3})*([zZ]{3})'
    
    $Path = "G:\Test\"  # Folder Containing the Text file 
                        # And where result Files will be placed
    
    $SourceData = Get-Content -Path "$($Path)SplitTest.txt"
    
    ForEach ($Line in $SourceData) {
    
      If ($Line -match $FNPattern) {  
        $FN = $Line.Trim() | ForEach-Object {$_.Substring(3,($_.Length-6))}  
        $CurrentFN = $Path + "$($FN.trim())" + ".txt" 
    
        If ($OldFileDelete.IsPresent -and (Test-Path -Path "$CurrentFN")) {
          Remove-Item -Path "$CurrentFN"
        }
      }
      Else {
        If ($StripBlankLines.IsPresent -and ($Line.Trim().Length -eq 0)) {
        }
        Else {
          Add-Content -Path "$CurrentFN" -Value $Line
        }
      }
    
    }  #End ForEach
    Of course you could also add a parameter for the directory and one for the source file also. The possibilities are endless.

    I hope this helps in your quest for PowerShell Mastery.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  13. The Following User Says Thank You to RetiredGeek For This Useful Post:

    pb89 (2016-03-04)

  14. #12
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    RG,

    what a treat thank you so much

    Have a great week end

    pb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •