String 用FART替换CFG文件中的通配符
我有一个用于数百个应用程序实例的配置文件,当前在任何单个实例上都是不同的。我需要将所有这些转换为使用相同的密码,并尝试使用“放屁”进行转换,但是我无法使用通配符 这是可能的,还是有其他工具可以更好地与命令行开关一起用于此目的 我正试着按照……的思路做些事情String 用FART替换CFG文件中的通配符,string,batch-file,powershell,replace,full-text-search,String,Batch File,Powershell,Replace,Full Text Search,我有一个用于数百个应用程序实例的配置文件,当前在任何单个实例上都是不同的。我需要将所有这些转换为使用相同的密码,并尝试使用“放屁”进行转换,但是我无法使用通配符 这是可能的,还是有其他工具可以更好地与命令行开关一起用于此目的 我正试着按照……的思路做些事情 fart "serverDZ.cfg" "passwordAdmin = \"*\";" "passwordAdmin = \"TEST\";" CFG文件如下所示 hostName = "Server-X"; password = "";
fart "serverDZ.cfg" "passwordAdmin = \"*\";" "passwordAdmin = \"TEST\";"
CFG文件如下所示
hostName = "Server-X";
password = "";
passwordAdmin = "qwerty";
timeStampFormat = "Short";
搜索并可选地替换文件中的字符串。它对整个文件进行操作 语法
ReplaceRegExp.vbs filename searchstring [replacestring]
剧本
On Error Resume Next
Set ShellApp = CreateObject("Shell.Application")
ReportErrors "Creating Shell.App"
set WshShell = WScript.CreateObject("WScript.Shell")
ReportErrors "Creating Wscript.Shell"
Set objArgs = WScript.Arguments
ReportErrors "Creating Wscript.Arg"
Set regEx = New RegExp
ReportErrors "Creating RegEx"
Set fso = CreateObject("Scripting.FileSystemObject")
ReportErrors "Creating FSO"
If objArgs.Count = 0 then
MsgBox "No parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 1 then
MsgBox "Only one parameter", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 2 then
Set srcfile = fso.GetFile(objArgs(0))
ReportErrors "srcFile"
If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
If err.number <> 0 then
Msgbox err.description & " " & srcFile.path, 48, "Serenity's Search"
err.clear
else
ReportErrors "TS" & " " & srcFile.path
Src=ts.readall
If err.number = 62 then
err.clear
else
ReportErrors "ReadTS" & " " & srcFile.path
regEx.Pattern = objArgs(1)
regEx.IgnoreCase = True
regEx.Global = True
If regEx.Test(Src) = True then
Msgbox "Found in " & srcfile.path, 64, "Serenity's Search"
End If
End If
End If
ReportErrors "Check OK" & " " & srcFile.path
Elseif objArgs.count = 3 then
Set srcfile = fso.GetFile(objArgs(0))
ReportErrors "srcFile"
If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
If err.number <> 0 then
Msgbox err.description & " " & srcFile.path, 48, "Serenity's Search"
err.clear
else
ReportErrors "TS" & " " & srcFile.path
Src=ts.readall
If err.number = 62 then
err.clear
else
ReportErrors "ReadTS" & " " & srcFile.path
regEx.Pattern = objArgs(1)
regEx.IgnoreCase = True
regEx.Global = True
NewSrc= regEx.Replace(Src, objArgs(2))
If NewSrc<>Src then
Msgbox "Replacement made in " & srcfile.path, 64, "Serenity's Search"
TS.close
Set TS = srcFile.OpenAsTextStream(2, 0)
ts.write newsrc
ReportErrors "Writing file"
End If
End If
End If
ReportErrors "Check OK" & " " & srcFile.path
Else
MsgBox "Too many parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ReportErrors "All Others"
End If
Sub ReportErrors(strModuleName)
If err.number<>0 then Msgbox "An unexpected error occurred. This dialog provides details on the error." & vbCRLF & vbCRLF & "Error Details " & vbCRLF & vbCRLF & "Script Name" & vbTab & Wscript.ScriptFullName & vbCRLF & "Module" & vbtab & vbTab & strModuleName & vbCRLF & "Error Number" & vbTab & err.number & vbCRLF & "Description" & vbTab & err.description, vbCritical + vbOKOnly, "Something unexpected"
Err.clear
End Sub
考虑到问题上有一个powershell标签,下面是如何在powershell中执行此操作
$FilePath = C:\FileToReplace.cfg
(Get-Content $FilePath) -replace 'passwordAdmin =.*','passwordAdmin = "Test"' | Out-File $FilePath
此脚本将获取$FilePath的内容,将任何与特定模式匹配的行(例如passwordAdmin=.*替换为passwordAdmin=Test)并将文件输出到原始文件。哇,这看起来是一个非常全面的工具,谢谢!因为我对正则表达式一点也不熟悉,所以我对如何使用它有点茫然。能否提供一个示例,说明如何搜索/替换passwordAdmin=和结尾之间的任何字符串;标记?括号、插入符号、管道和反斜杠用反斜杠漏掉。狗找到了狗\d数字通配符查找一个数字\\d查找\d。
$FilePath = C:\FileToReplace.cfg
(Get-Content $FilePath) -replace 'passwordAdmin =.*','passwordAdmin = "Test"' | Out-File $FilePath