Windows Command Line – Regex Replace from Command Line

command linefind and replaceregexwindows

Is it possible to regex replace from command line? Right now I'm using notepad++ to do this. In command line I can only use FINDSTR wich can only locate the mached files/lines

EDIT :
Maybe it will be possible to make a VB script and run it from cmd? I just created a file "hi.vbs" with the content

Msgbox "Hi Buddy"

Then cmd allows me to run it directly by typing "hi" from command line.

So if it is not possible with batch script, then i may use a VB script trough batch. Or..?

Best Answer

go here
http://gnuwin32.sourceforge.net/packages.html
scroll down to SED. Download coreutils too while you're at it.

this command will replace a with b globally, and on each line. so not just the first occurrence on the line.

e.g. using sed "s/a/b/g" a.txt

C:\>type a.txt
aaaa
aaa

C:\>sed "s/a/b/" a.txt
baaa
baa

C:\>sed "s/a/b/g" a.txt
bbbb
bbb

C:\>

VBScript does support regular expressions, you can do find and replace with it.

dim re, s
set re = new RegExp

re.Pattern="in"
re.Global=True 'false is default
s="bin din in"
MsgBox re.Replace(s,"xxx")

Displays bxxx dxxx xxx

Related Question