我正在使用Windows命令行环境编写批处理文件脚本,想要将文件中每个文本实例(例如 "FOO")更改为另一个文本(例如 "BAR")。最简单的方法是什么?是否有任何内置函数可以使用?
我正在使用Windows命令行环境编写批处理文件脚本,想要将文件中每个文本实例(例如 "FOO")更改为另一个文本(例如 "BAR")。最简单的方法是什么?是否有任何内置函数可以使用?
当您使用Windows上的Git时,只需启动git-bash
并使用sed
。或者,在使用Windows 10时,启动Linux子系统中的“Bash on Ubuntu on Windows”并使用sed
。
sed
是一种流编辑器,但可以通过以下命令直接编辑文件:
sed -i -e 's/foo/bar/g' filename
-i
选项用于就地编辑文件。-e
选项指示要运行的命令。
s
用于将找到的表达式“foo”替换为“bar”,g
用于替换所有找到的匹配项。说明:
如果您想仅在Git存储库的版本化文件中替换字符串,则可以使用:
git ls-files <可能的子文件夹和筛选器> | xargs sed -i -e 's/foo/bar/g'
这非常有效。
我尝试了一些现有的答案,但更喜欢我的改进解决方案...
type test.txt | powershell -Command "$input | ForEach-Object { $_ -replace \"foo\", \"bar\" }"
或者,如果你想将输出再次保存到文件中...
type test.txt | powershell -Command "$input | ForEach-Object { $_ -replace \"foo\", \"bar\" }" > outputFile.txt
这样做的好处是你可以从任何程序中获取输出。还会研究如何使用正则表达式。但我无法弄成BAT文件以便更轻松地使用... :-(
我用过Perl,它非常出色。
perl -pi.orig -e "s/<textToReplace>/<textToReplaceWith>/g;" <fileName>
.orig是它会附加到原始文件的扩展名
对于多个匹配类似于*.html的文件
for %x in (<filePattern>) do perl -pi.orig -e "s/<textToReplace>/<textToReplaceWith>/g;" %x
perl -pi.backup -e
替换sed
,并感谢它 :) - Yann391) 使用e?
选项,该选项将评估特殊字符序列,如\n\r
和unicode序列。在这种情况下,将替换引用的"Foo"
和"Bar"
:
call replacer.bat "e?C:\content.txt" "\u0022Foo\u0022" "\u0022Bar\u0022"
2)直接替换,不需要引用 Foo
和 Bar
。
call replacer.bat "C:\content.txt" "Foo" "Bar"
call replacer.bat "content.txt" "foo" "bar" "xyz abc" "bla fart"...
- acgbox这里是我在Win XP上找到的解决方案。在我的批处理文件中,我包含了以下内容:
set value=new_value
:: Setup initial configuration
:: I use && as the delimiter in the file because it should not exist, thereby giving me the whole line
::
echo --> Setting configuration and properties.
for /f "tokens=* delims=&&" %%a in (config\config.txt) do (
call replace.bat "%%a" _KEY_ %value% config\temp.txt
)
del config\config.txt
rename config\temp.txt config.txt
replace.bat
文件内容如下。我没有找到将该函数包含在同一批处理文件中的方法,因为%%a
变量似乎总是给出for循环中的最后一个值。
@echo off
:: This ensures the parameters are resolved prior to the internal variable
::
SetLocal EnableDelayedExpansion
:: Replaces Key Variables
::
:: Parameters:
:: %1 = Line to search for replacement
:: %2 = Key to replace
:: %3 = Value to replace key with
:: %4 = File in which to write the replacement
::
:: Read in line without the surrounding double quotes (use ~)
::
set line=%~1
:: Write line to specified file, replacing key (%2) with value (%3)
::
echo !line:%2=%3! >> %4
:: Restore delayed expansion
::
EndLocal
Thissed-like
helper batch file is called repl.bat
(by dbenham).
Example using the L
literal switch:
echo This is FOO here|repl "FOO" "BAR" L
echo and with a file:
type "file.txt" |repl "FOO" "BAR" L >"newfile.txt"
This grep-like
helper batch file is called findrepl.bat
(by aacini).
Example which has regular expressions active:
echo This is FOO here|findrepl "FOO" "BAR"
echo and with a file:
type "file.txt" |findrepl "FOO" "BAR" >"newfile.txt"
当放置在路径上的文件夹中,或者与批处理文件或从cmd提示符中使用时,它们都成为功能强大的系统级实用程序。
它们都具有不区分大小写的开关和许多其他功能。
我更喜欢使用GNU Windows实用程序中的sed
,需要注意以下几点:
- 在Windows中,单引号
''
无法正常工作,请改用双引号""
sed -i
在Windows中无法正常工作,需要进行文件交换
因此,在Windows中查找和替换文件中文本的有效sed
代码如下:
sed -e "s/foo/bar/g" test.txt > tmp.txt && mv tmp.txt test.txt
可能有点晚了,但我经常寻找类似的东西,因为我不想经历软件审核的痛苦。
然而,通常你会使用各种形式的FOR语句。有人创建了一个有用的批处理文件来进行搜索和替换。请看这里。重要的是要了解提供的批处理文件的限制。因此,我不在此答案中复制源代码。
Power shell命令非常好用
(
test.txt | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt
)
git ls-files <eventual subfolders & filters> | xargs sed -i -e 's/foo/bar/g'
,这将奏效。 - ereOn