使用命令替换文本多行内容_从命令行替换纯文本文件中的文本

使用命令替换文本多行内容_从命令行替换纯文本文件中的文本

使用命令替换文本多行内容

A very useful function which is missing from the Windows library of command line tools is the ability to replace text in plain text files. A function like this can be used for a variety of practical tasks which many system admin’s perform, such as:

Windows命令行工具库中缺少的一个非常有用的功能是能够替换纯文本文件中的文本。 这样的功能可用于许多系统管理员执行的各种实际任务,例如:

  • Update configuration/INI files to replace UNC paths.

    更新配置/ INI文件以替换UNC路径。
  • Mass update user information stored in INI files on a Terminal/Citrix server.

    在终端/ Citrix服务器上的INI文件中存储的批量更新用户信息。
  • Use in conjunction with scripts to deploy ‘templated’ data and then apply values to the copied files.

    与脚本结合使用,以部署“模板化”数据,然后将值应用于复制的文件。

Our solution is a VBScript which interfaces with the Visual Basic Replace function. By placing this script into a location in your Windows PATH variable, you now have this functionality available at your disposal.

我们的解决方案是一个VBScript,它与Visual Basic替换功能对接。 通过将此脚本放置在Windows PATH变量中的某个位置,您现在可以使用此功能。

用途 (Uses)

Once on your system, you can call the script by simply using the ReplaceText command. A few examples will illustrate ways you can use this:

进入系统后,只需使用ReplaceText命令即可调用脚本。 一些示例将说明您可以使用此方法:

Replace the word “null” with “n/a” in the C:DataValues.csv file:

在C:DataValues.csv文件中,将单词“ null”替换为“ n / a”:

ReplaceText “C:DataValues.csv” null n/a

ReplaceText“ C:DataValues.csv” null不适用

Scan all INI files in the C:Users (+ sub directories) folder replacing all occurrences of “Server=Old” with “Server=New” using a case insensitive search:

使用不区分大小写的搜索,扫描C:Users(+子目录)文件夹中的所有INI文件,将所有出现的“ Server = Old”替换为“ Server = New”:

FORFILES /P “C:Users” /M *.ini /S /C “Cmd /C ReplaceText @path Server=Old Server=New /I”

FORFILES / P“ C:Users” / M * .ini / S / C“ Cmd / C ReplaceText @path Server = Old Server = New / I”

Scan all CFG files in the current user’s profile replacing “[email protected]” with “PA$$woRd” using a case sensitive search:

使用区分大小写的搜索来扫描当前用户配置文件中的所有CFG文件,并用“ PA $$ woRd”替换“ p @ ssw0rd”:

FORFILES /P “%UserProfile%” /M *.cfg /S /C “Cmd /C ReplaceText @path [email protected] PA$$woRd”

FORFILES / P“%UserProfile%” / M * .cfg / S / C“ Cmd / C ReplaceText @path p @ ssw0rd PA $$ woRd”

As you can see below, the script is very simple and can easily be modified to accommodate any special situations you may have. Alternately, you may want to create copies of the script which hardcode particular values so you can execute the command with a double-click and/or allow you to easily distribute it to others.

如下所示,该脚本非常简单,可以轻松修改以适应您可能遇到的任何特殊情况。 或者,您可能想要创建对特定值进行硬编码的脚本副本,以便可以双击执行命令和/或轻松地将其分发给其他人。

剧本 (The Script)

'Replace Text'Written by: Jason Faulkner'SysadminGeek.com

“替换文字”作者:Jason Faulkner'SysadminGeek.com

'This script should be placed in a folder specified in your system's PATH variable.

“此脚本应放置在系统的PATH变量中指定的文件夹中。

'Usage (WScript):'ReplaceText FileName OldText NewText [/I]

'用法(WScript):'ReplaceText FileName OldText NewText [/ I]

' /I (optional) - Text matching is not case sensitive

'/ I(可选)-文本匹配不区分大小写

Set oArgs = WScript.Arguments

设置oArgs = WScript.Arguments

intCaseSensitive = 0For i = 3 to oArgs.Count-1    If UCase(oArgs(i)) = "/I" Then intCaseSensitive = 1Next

intCaseSensitive = 0对于i = 3到oArgs.Count-1如果UCase(oArgs(i))=“ / I”,则intCaseSensitive = 1下一个

Set oFSO = CreateObject("Scripting.FileSystemObject")

设置oFSO = CreateObject(“ Scripting.FileSystemObject”)

If Not oFSO.FileExists(oArgs(0)) Then    WScript.Echo "Specified file does not exist."Else    Set oFile = oFSO.OpenTextFile(oArgs(0), 1)    strText = oFile.ReadAll    oFile.Close

如果不是oFSO.FileExists(oArgs(0)),则WScript.Echo“指定的文件不存在。”否则设置oFile = oFSO.OpenTextFile(oArgs(0),1)strText = oFile.ReadAll oFile.Close

    strText = Replace(strText, oArgs(1), oArgs(2), 1, -1, intCaseSensitive)

strText = Replace(strText,oArgs(1),oArgs(2),1,-1,intCaseSensitive)

    Set oFile = oFSO.OpenTextFile(oArgs(0), 2)    oFile.WriteLine strText    oFile.CloseEnd If

设置oFile = oFSO.OpenTextFile(oArgs(0),2)oFile.WriteLine strText oFile.CloseEnd如果

补充笔记 (Additional Notes)

By default, Windows uses WScript to execute VBScript (VBS) files. The only problem this can cause is any errors and/or messages from the script will appear as popup boxes. For a command line tool, it is best these messages be displayed in the console. There are a couple of ways you can accomplish this.

默认情况下,Windows使用WScript执行VBScript(VBS)文件。 这可能导致的唯一问题是任何错误和/或来自脚本的消息将显示为弹出框。 对于命令行工具,最好将这些消息显示在控制台中。 有两种方法可以完成此操作。

Change the default handler of VBScript files to CScript by running this command from command prompt (with Administrator rights):

通过从命令提示符(具有管理员权限)中运行以下命令,将VBScript文件的默认处理程序更改为CScript:

CScript //H:CScript

CScript // H:CScript

Run the ReplaceText script explicitly using the CScript command:

使用CScript命令显式运行ReplaceText脚本:

CScript “C:PathToReplaceText.vbs” //B FileName OldText NewText [/I]

CScript“ C:PathToReplaceText.vbs” // B FileName OldText NewText [/ I]

As a special case, executing ReplaceText from a batch script typically implies CScript as the engine used regardless of the default handler. You will definitely want to test this though prior to relying on this functionality.

作为一种特殊情况,从批处理脚本执行ReplaceText通常意味着CScript是所使用的引擎,而与默认处理程序无关。 您一定要通过此功能对其进行测试。

Download ReplaceText Script from SysadminGeek.com

从SysadminGeek.com下载ReplaceText脚本

翻译自: https://www.howtogeek.com/51194/replace-text-in-plain-text-files-from-the-command-line/

使用命令替换文本多行内容