5

I have a shell script (.sh) where I use the sed command to replace the last matching character in a file(I have placed the command below).

How should I replace the command to get the same functionality in a batch file (.bat) without adding any packages/plugins (To be compatible in windows 7 and later)?

sed -i '$ s/,/;/g' abc.java
Cyrus
  • 84,225
  • 14
  • 89
  • 153

2 Answers2

5

Windows batch scripting does not have any native tools that can conveniently do anything similar to sed.

You could probably use PowerShell with a simple script, or CSCRIPT with a small custom VBS or JScript script.

I would use JREPL.BAT - a regular expression text processing utility. It is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. It does not require any non-native exe or com files.

Full documentation is available from the command line using jrepl /?, or jrepl /?? for paged help.

Your simple task could be performed by JREPL using:

call jrepl "," ";" /inc -1 /f abc.java /o -
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 1
    Should I install anything to my machine to use jrepl? My machine has windows 7 – Prakadeessh Arunachalam Sep 30 '16 at 11:48
  • 1
    @PrakadeesshArunachalam - You just need to copy the code found at the link and paste it into a file named JREPL.BAT. The file must be saved as ASCII (not Unicode). It is most convenient if you put JREPL.BAT in a folder that is listed within your PATH. – dbenham Sep 30 '16 at 13:32
0

There is no sed in Windows, not without any plugin or add-on. If you need to use sed, you can install SED for Windows.

tripleee
  • 175,061
  • 34
  • 275
  • 318
AFR
  • 105
  • 4