2015-11-03 102 views
2

我想使用PowerShell根據標題的位置自動將SQL文件劃分爲單獨的文件。使用PowerShell,SQL文件如何根據其內容分割成多個文件?

被分裂的一個例子SQL文件如下:

/**************************************** 
Section 1 
****************************************/ 
Select 1 

/**************************************** 
Section 2 
****************************************/ 
Select 2 

/**************************************** 
Section 3 
****************************************/ 
Select 3 

我想新的文件將被命名爲每個文件即「第1條」一節的標題,「第2」和「科3' 。第一個文件的內容應該如下:

/**************************************** 
Section 1 
****************************************/ 
Select 1 

字符串:/****************************************僅在爲副標題的SQL文件中使用,因此可用於標識節的開始。文件名將始終是正下方的文本。

+3

你嘗試過什麼了嗎? –

+0

@Mike不,因爲我是PowerShell的新手。它看起來可以使用.NET StreamReader類和「Add-Content」cmdlet完成,如下面問題的答案:「http://stackoverflow.com/questions/1001776/how-can-i-split -a文本文件 - 使用 - PowerShell的」。但是,這個問題與我的不同之處在於我能夠使用答案。 – Fletch

回答

1

下面的代碼使用註釋塊中找到的標題名稱。它還根據註釋塊的位置將SQL文件分割成多個SQL文件。

#load SQL file contents in an array 
$SQL = Get-Content "U:\Test\FileToSplit.sql" 
$OutputPath = "U:\TestOutput" 

#find first section name and count number of sections 
$sectioncounter = 0 
$checkcounter = 0 
$filenames = @() 

$SQL | % { 
    #Add file name to array if new section was found on the previous line 
    If ($checkcounter -lt $sectioncounter) 
    { 
     $filenames += $_ 
     $checkcounter = $sectioncounter 
    } 
    Else 
    { 
     If ($_.StartsWith("/*")) 
     { 
      $sectioncounter += 1 
     } 
    } 
} 

#return if too many sections were found 
If ($sectioncounter > 50) { return "Too many sections found"} 

$sectioncounter = 0 
$endcommentcounter = 0 

#for each line of the SQL file (Ref: sodawillow) 
$SQL | % { 

    #if new comment block is found point to next section name, unless its the start of the first section 
    If ($_.StartsWith("/*") -And ($endcommentcounter -gt 0)) 
    { 
     $sectioncounter += 1 
    } 

    If ($_.EndsWith("*/")) 
    { 
     $endcommentcounter += 1 
    } 

    #build output path 
    $tempfilename = $filenames[$sectioncounter] 
    $outFile = "$OutputPath\$tempfilename.sql" 

    #push line to the current output file (appending to existing contents) 
    $_ | Out-File $outFile -Append 
} 
2

你可以嘗試這樣的(分開這裏基於部分之間的空行):

#create an index for our output files 
$fileIndex = 1 

#load SQLite file contents in an array 
$sqlite = Get-Content "G:\input\sqlite.txt" 

#for each line of the SQLite file 
$sqlite | % { 


    if($_ -eq "") { 

     #if the line is empty, increment output file index to create a new file 
     $fileindex++ 

    } else { 

     #if the line is not empty 
     #build output path 
     $outFile = "G:\output\section$fileindex.txt" 

     #push line to the current output file (appending to existing contents) 
     $_ | Out-File $outFile -Append 

    } 

} 

#load generated files in an array 
$tempfiles = Get-ChildItem "G:\output" 

#for each file 
$tempfiles | % { 

    #load file contents in an array 
    $data = Get-Content $_.FullName 

    #rename file after second line contents 
    Rename-Item $_.FullName "$($data[1]).txt" 
} 
+0

+1,因爲這個答案對我很有幫助。我已經添加了我的最終答案,因爲我已經修改並添加了腳本的各個部分,以便在我的實際SQL文件中正確地查找段名稱和中斷,這些文件可能包含段中的多個行返回。 – Fletch

相關問題