0

I am trying to replace the text 'HEADER' within a file with the control character new page (FF - Hex value = C). I was able to do this using SED as shown below:

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

I want to be able to do this using JREPL.BAT. I have try the following without success:

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

The reason why I want to do it with jrepl is to avoid having to install SED in everyone's computer who will eventually need to run the script. Any ideas on how to do this?

dbenham
  • 127,446
  • 28
  • 251
  • 390
Ezy
  • 17
  • 6

1 Answers1

1

You don't need a several hundred lines Batch file to perform a replacement as simple as this one. The two-lines Batch file below do the same:

@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"));

For a further description of regular expression syntax, see this page.

EDIT

In order to easily test this program, you may modify it in this way:

@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"));

Then, open a command-prompt session and execute it from the command line in order to see in the screen any error message. For example:

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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Aacini, not sure what I'm doing wrong. I put these two lines of code in a batch file in the same directory where myfile.txt resides. Not sure if I needed to change something else as it doesn't seem to work. – Ezy Feb 01 '17 at 14:52
  • See the edit in my answer. I suggest you to do _exactly the same_ and then report the errors that appear in the screen, because "it doesn't seem to work" can't help... – Aacini Feb 01 '17 at 16:17
  • Excellent Aacini, It worked! This is a nice way of doing the same thing as with JREPL.bat, but without having to call the executable file. Thank you very much. – Ezy Feb 01 '17 at 21:15