我正在整理一个简单的 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 盒子。