1
我正在放置一個簡單的PowerShell腳本,它將查找/替換各種數量的關鍵字。如果找到關鍵字,則應該用不同的短語替換它們。ForEach-Object使用Powershell從文件中刪除行
這適用於存在關鍵字的行,但腳本正在刪除關鍵字不存在的行;這不是我想要的行爲。
我的期望是它會取代KEYWORDS並保持所有其他東西不變。
腳本:
#*=============================================
#* Script Name: SQLFILE_find_and_replace.ps1
#* Created: 12-Jun-2013
#*=============================================
#* Purpose: Intended for SQLFILE scripts
#* generated from a datapump dump file.
#*
#* Will find and replace "Create [Keyword]"
#* with "Create or Replace [Keyword]" within a
#* specified file.
#*=============================================
#Keywords for find/replace to act on; adjust as necessary.
$KEYWORDS = @("PROCEDURE","PACKAGE","FUNCTION","TYPE","SEQUENCE","VIEW","DATABASE LINK","TRIGGER")
# Get-FileName from http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
#Ask user for file
$FILENAMELOC=Get-FileName -initialDirectory "C:\"
Write-Progress -Activity "Loading file" -status "$FILENAMELOC" -percentComplete 0
$file = (Get-Content -Path $FILENAMELOC)
$lines = ($file | Measure-Object)
$lineprocessed = 0
$file |
ForEach-Object {
$lineprocessed = $lineprocessed + 1
ForEach ($KEYWORD in $KEYWORDS) {
if ($_.indexof("$KEYWORD") -ge 0) {
if ($KEYWORD -eq "SEQUENCE" -and $_.indexof("MINVALUE") -ge 0) {
$SEQUENCENAME = $_.Substring($_.indexof("$KEYWORD"),$_.indexof("MINVALUE")-$_.indexof("$KEYWORD")-1)
$DROP = "DROP "+$SEQUENCENAME +";"
$CREATE = "CREATE "+$SEQUENCENAME +";"
$SQL = $DROP+"`r`n"+$CREATE
$_ -replace "CREATE $KEYWORD", $SQL
}
else {
$_ -replace "CREATE $KEYWORD", "CREATE OR REPLACE $KEYWORD"
}
}
}
Write-Progress -Activity "Replacing keywords" -status "Running" -percentComplete ([System.Math]::Round((($lineprocessed/$lines.count)*100),2))
} |
Set-Content $FILENAMELOC
測試文件:
This is a test.
CREATE PROCEDURE Blah
CREATE TRIGGER Boblawbla
CREATE FUNCTION
This is another test
Watch me disappear.
結果:
CREATE OR REPLACE PROCEDURE Blah
CREATE OR REPLACE TRIGGER Boblawbla
CREATE OR REPLACE FUNCTION
這是一個Windows 7箱。
其實,寫主機是我的「調試」的方式,我並沒有考慮將管道設置的內容,但我刪除即從上面去除歧義。 –
你說得很對。但是,如果我在那裏放了一個else,而它爲每行添加一個結果,它會這樣做8次(每個關鍵字一次)。你有建議如何解決這個問題嗎?我覺得我被困在兩個選項中的一個之間 - 循環遍歷文件8次(每個關鍵字一次)或循環一次文件,並循環遍歷每行8次。你的解釋是非常有幫助的,我確實認爲 - 取而代之的是$ _;關鍵是要知道情況並非如此。感謝那 –
查看更新的答案。 –