Sunday 2 February 2020

windows - Regex Replace from Command line


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..?



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


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...