Search code examples
batch-filesedjrepl

Replace control character with JREPL.bat


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?


Solution

  • 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