用 JREPL.bat 替换控制字符

Replace control character with JREPL.bat

我正在尝试用控制字符新页面(FF - 十六进制值 = C)替换文件中的文本 'HEADER'。我能够使用 SED 执行此操作,如下所示:

sed -I "s/HEADER/\xC/g" c:\myfile.txt  

我希望能够使用 JREPL.BAT 执行此操作。我尝试了以下但没有成功:

c:\jrepl "HEADER" "\xC" /f myfile.txt /o -
c:\jrepl "HEADER" "\xC" /x /f myfile.txt /o -

我想用 jrepl 来做的原因是为了避免在最终需要 运行 脚本的每个人的计算机上安装 SED。 关于如何做到这一点有什么想法吗?

您不需要几百行批处理文件来执行像这个一样简单的替换。 two-lines 下面的批处理文件做同样的事情:

@set @a=0 // & cscript //nologo //E:JScript "%~F0" < myfile.txt > out.txt & move /Y out.txt myfile.txt & goto :EOF 

WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/HEADER/g,"\x0C"));

有关正则表达式语法的进一步说明,请参阅 this page

编辑

为了方便测试本程序,您可以这样修改:

@set @a=0 /*
cscript //nologo //E:JScript "%~F0" < myfile.txt > out.txt
move /Y out.txt myfile.txt
goto :EOF */

WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/HEADER/g,"\x0C"));

然后,打开一个 command-prompt 会话并从命令行执行它,以便在屏幕上看到任何错误消息。例如:

C:>\Users\Antonio\test type myfile.txt
This is a data line
HEADER This is the first line at top of page
This is more data line


C:>\Users\Antonio\test test.bat

C:>\Users\Antonio\test cscript //nologo //E:JScript "C:\Users\Antonio\test\test.bat"  0<myfile.txt 1>out.txt

C:>\Users\Antonio\test move /Y out.txt myfile.txt
Se han movido         1 archivos.

C:>\Users\Antonio\test goto :EOF */


C:>\Users\Antonio\test type myfile.txt
This is a data line
♀ This is the first line at top of page
This is more data line