查找并替换文件中键/值对的值

时间:2022-04-03 23:15:29

I want to search for a value in a text file and replace it, like so:

我想在文本文件中搜索一个值并替换它,如下所示:

Search for value= finds:

搜索value = found:

value=1122

Replace the value (1122) with something else (3344), result:

用其他东西(3344)替换值(1122),结果:

value=3344

Also, replacing what's left of the entire line would work just fine, meaning whatever comes after value= is also replaced with 3344.

另外,替换整行的剩余部分也可以正常工作,这意味着在值=之后的任何内容也会被替换为3344。

1 个解决方案

#1


Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
'Remove ^ from quoting command line. Quote, ampersand and brackets
Pttn = Replace(Arg(2), "^(", "(")
Pttn = Replace(Pttn, "^)", ")")
Pttn = Replace(Pttn, "^&", "&")
Pttn = Replace(Pttn, "^""", """")
Set regEx1 = New RegExp
If Instr(LCase(Arg(1)), "i") > 0 then
    regEx1.IgnoreCase = True
Else
    regEx1.IgnoreCase = False
End If 
regEx1.Global = False
regEx1.Pattern = Pttn 
Do Until Inp.AtEndOfStream
    Line=Inp.readline
    Line = RegEx1.Replace(Line, Arg(3)) 
    outp.writeline Line
Loop

To use

cscript //nologo c:\folder\filter.vbs replace i "=" "No equal sign" < "%systemroot%\win.ini" > OutputFile.txt

Filter reads and writes standard in and standard out only. These are only available in a command prompt.

过滤器仅读取和写入标准输入和标准输出。这些仅在命令提示符中可用。

filter <inputfile >outputfile
filter <inputfile | other_command
other_command | filter >outputfile
other_command | filter | other_command

Replace

filter replace {i|n} expression replace
filter repl {i|n} expression replace

Finds and replaces text using regular expressions.

使用正则表达式查找和替换文本。

Also used to extract substrings from a file.

还用于从文件中提取子字符串。

Ampersands and brackets in expression must be escaped with the caret. Do not escape carets. Use hexidecimal code \x22 for quotes.

表达式中的&符号和括号必须使用插入符号进行转义。不要逃避插入。使用十六进制代码\ x22作为引号。

SearchOptions

i - ignore case
n - none

Expression

https://msdn.microsoft.com/en-us/subscriptions/f97kw5ka(v%3Dvs.84).aspx

Replace

The text to replace. Use $1, $2, $..., $n to specify sub matches in the replace string

要替换的文本。使用$ 1,$ 2,$ ...,$ n指定替换字符串中的子匹配

Example

filter replace i "=" "No equal sign" < "%systemroot%\win.ini"

This searches for text within square brackets and replaces the line with cat followed by the text within brackets

这将在方括号内搜索文本,并用cat替换该行,后跟括号内的文本

Filter replace i "^\[^(.*^)\]" "cat$1" < %windir%\win.ini

This searches for any text and prints from the 11th character to the end of the line.

这将搜索从第11个字符到行尾的任何文本和打印。

Filter replace i "^.{10}^(.*^)$" "$1" < %windir%\win.ini

This searches a CSV file and prints the second and fourth field

这将搜索CSV文件并打印第二个和第四个字段

Filter replace i "^.+,^(.+^),.+,^(.+^)$" "$1,$2" < csv.txt

#1


Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
'Remove ^ from quoting command line. Quote, ampersand and brackets
Pttn = Replace(Arg(2), "^(", "(")
Pttn = Replace(Pttn, "^)", ")")
Pttn = Replace(Pttn, "^&", "&")
Pttn = Replace(Pttn, "^""", """")
Set regEx1 = New RegExp
If Instr(LCase(Arg(1)), "i") > 0 then
    regEx1.IgnoreCase = True
Else
    regEx1.IgnoreCase = False
End If 
regEx1.Global = False
regEx1.Pattern = Pttn 
Do Until Inp.AtEndOfStream
    Line=Inp.readline
    Line = RegEx1.Replace(Line, Arg(3)) 
    outp.writeline Line
Loop

To use

cscript //nologo c:\folder\filter.vbs replace i "=" "No equal sign" < "%systemroot%\win.ini" > OutputFile.txt

Filter reads and writes standard in and standard out only. These are only available in a command prompt.

过滤器仅读取和写入标准输入和标准输出。这些仅在命令提示符中可用。

filter <inputfile >outputfile
filter <inputfile | other_command
other_command | filter >outputfile
other_command | filter | other_command

Replace

filter replace {i|n} expression replace
filter repl {i|n} expression replace

Finds and replaces text using regular expressions.

使用正则表达式查找和替换文本。

Also used to extract substrings from a file.

还用于从文件中提取子字符串。

Ampersands and brackets in expression must be escaped with the caret. Do not escape carets. Use hexidecimal code \x22 for quotes.

表达式中的&符号和括号必须使用插入符号进行转义。不要逃避插入。使用十六进制代码\ x22作为引号。

SearchOptions

i - ignore case
n - none

Expression

https://msdn.microsoft.com/en-us/subscriptions/f97kw5ka(v%3Dvs.84).aspx

Replace

The text to replace. Use $1, $2, $..., $n to specify sub matches in the replace string

要替换的文本。使用$ 1,$ 2,$ ...,$ n指定替换字符串中的子匹配

Example

filter replace i "=" "No equal sign" < "%systemroot%\win.ini"

This searches for text within square brackets and replaces the line with cat followed by the text within brackets

这将在方括号内搜索文本,并用cat替换该行,后跟括号内的文本

Filter replace i "^\[^(.*^)\]" "cat$1" < %windir%\win.ini

This searches for any text and prints from the 11th character to the end of the line.

这将搜索从第11个字符到行尾的任何文本和打印。

Filter replace i "^.{10}^(.*^)$" "$1" < %windir%\win.ini

This searches a CSV file and prints the second and fourth field

这将搜索CSV文件并打印第二个和第四个字段

Filter replace i "^.+,^(.+^),.+,^(.+^)$" "$1,$2" < csv.txt