2

i'm reading a lot of threads - but nothing for me personal. I need to split text file in the following form:

---------------------  Instance Type and Transmission --------------    
...text..     
...text.. 
--------------------------- Message Trailer ------------------------    
...text...
...text...      
---------------------  Instance Type and Transmission --------------
...text.. 
...text.. 

dividing content by lines ------------- Instance Type and Transmission -------------- and output text between in newer file.

Like this:

File1:

---------------------  Instance Type and Transmission --------------    
    ...text..     
    ...text.. 
    --------------------------- Message Trailer ------------------------    
    ...text...
    ...text...  

File2:

---------------------  Instance Type and Transmission --------------
...text.. 
...text.. 

Perl and awk do this pretty simple and i found some examples, and nothing in powershell, only text file by size splitting.

Thanks to @CB. I ended with this solution valid for multiple files:

  $InPC = "C:\Scripts"
Get-ChildItem -Path $InPC -Filter *.txt | ForEach-Object -Process { 
        $basename= $_.BaseName   
        $m = ( ( Get-Content $_.FullName | Where { $_ | Select-String "---------------------  Instance Type and Transmission --------------" -Quiet } | Measure-Object | ForEach-Object { $_.Count } ) -ge 2) 
        $a = 1
        if ($m) {
  Get-Content $_.FullName | % {

    If ($_ -match "---------------------  Instance Type and Transmission --------------") {
        $OutputFile = "$InPC\$basename _$a.txt"
        $a++
    }    
    Add-Content $OutputFile $_
    }
  Remove-Item $_.FullName 
  }
  }
Magsky
  • 27
  • 1
  • 5

1 Answers1

3

Something like this should work:

$InputFile = "c:\path\myfiletosplit.txt"
$Reader = New-Object System.IO.StreamReader($InputFile)
$a = 1
While (($Line = $Reader.ReadLine()) -ne $null) {
    If ($Line -match "---------------------  Instance Type and Transmission --------------") {
        $OutputFile = "MySplittedFileNumber$a.txt"
        $a++
    }    
    Add-Content $OutputFile $Line
}

or whitout .net class:

$a = 1
gc "c:\path\myfiletosplit.txt" | % {    
    If ($_ -match "---------------------  Instance Type and Transmission --------------") {
        $OutputFile = "MySplittedFileNumber$a.txt"
        $a++
    }    
    Add-Content $OutputFile $_
}
CB.
  • 58,865
  • 9
  • 159
  • 159
  • Second option i didn't try because first working like a charm. Thank's a lot! – Magsky Jun 16 '14 at 13:05
  • CB. please help in newer parameters: what to do when i work with multiple files - and want to use basename and increment it ? – Magsky Jun 16 '14 at 16:25
  • Something like that: Get-ChildItem *.prt | ForEach-Object -Process { $basename= % {$_.BaseName} $a=1 Get-Content $_ | % { If ($_ -match "--------------------- Instance Type and Transmission --------------") { $OutputFile = "$basename_$a.prt" $a++ } Add-Content $OutputFile $_ } } How did you guess it doesn't work – Magsky Jun 16 '14 at 16:28
  • haha i'm found that if i remove `_` in `"$basename_$a.prt"` my variant is working. Thank you anyway. – Magsky Jun 17 '14 at 05:49
  • P.S. you can add my variant in your answer in case where need to work with multiple files for sharing to other people. – Magsky Jun 17 '14 at 05:56
  • @Magsky if you want to use the `_` as separator try like this: `"$basename`_$a.prt"` . This to tell to pawershell that the variable is `$basename` and not `$basename_`. the underscore char is a valid char for variable! – CB. Jun 17 '14 at 06:59
  • Thanks for advice! But i'm stuck again. I'm edit first question to explain what reason of this. – Magsky Jun 17 '14 at 09:01
  • @Magsky try changing `$basename= % {$_.BaseName}` to `$basename= $_.fullName` – CB. Jun 17 '14 at 10:12
  • Changing did not fixed problem...( – Magsky Jun 17 '14 at 10:43
  • @Magsky are you sure that "C:\Users\a.ulianov\test.prt" exists? – CB. Jun 17 '14 at 10:48
  • This path doesn't exists but for what reason that path needed to work ? why executing script access to this path? my test files `PRTPRT.prt test.prt` locates at C:\Scripts. I'm using Powershell ISE and after rebooting this path changed to C:\Program Files\Microsoft Office\Office14. I'm assume that default PS directory. – Magsky Jun 17 '14 at 10:58