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

link|improve this question

At the end you put an inconclusive sentence and the whole EDIT part isn't pointing to the problem is more like "I create a hello world in vb, can i create something like that in batch? Check the answer posted by @IanPugsley is the easy way to do regex-replace. – voodoomsr Sep 29 '11 at 11:10
@Aziz: has any of these answers worked for you? If so, would you accept that one? That's part of participating in this community; the way we thank one another for help. The reputation points given for an accepted answer help other users assess one another's experience. Thanks. – JRobert Sep 29 '11 at 21:25
I was thinking to call somthing like MYVBREPLACE(.vbs) FILENAME REGEXPATTERN REPLACEMENT from command line. – Aziz Oct 5 '11 at 13:00
feedback

4 Answers

up vote 2 down vote accepted

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

link|improve this answer
Amazing crazy tool!!! This tool was too adwanced. I could not manage to use it yet, but when I read gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command I can see that this can do much more than what I could expect. Thanks a lot – Aziz Oct 5 '11 at 12:40
New versions of bash can also do regular expressions, sometimes that's enough for simple tasks and there's no need to use even sed. – vtest Nov 24 '11 at 6:13
feedback

The Scripting Guy covers how to do this in PowerShell (no additional downloads on most recent Windows OSs, you probably already have it installed).

Start it up, run the following (to replace a * with a @):

(Get-Content C:\Scripts\Test.txt) | 
Foreach-Object {$_ -replace "\*", "@"} | 
Set-Content C:\Scripts\Test.txt

This supports .NET regular expressions, including positive and negative look-ahead, and all manner of things notepad++ does not support with regex (as much as I love notepad++).

link|improve this answer
feedback

Two good choices are either Cygwin which will give you a Unix-like, bash-like environment (alternative to cmd.exe); and Unxutils, Win32 ports of a collection of individual Unix utilities. In either package, see 'sed', 'awk', and 'grep'.

link|improve this answer
unxutils is old. now the thing is gnuwin32 – barlop Oct 4 '11 at 14:10
Thank you JRobert for your suggestion. Sorry that I did not understod it, but now I can see that SED was the thing. – Aziz Oct 5 '11 at 12:55
feedback

Looks like you can use regex with FINDSTR

findstr [Windows CMD]:

findstr allows to search for text (as specified with pattern in the file file-name. If file-name contains wildcards (* or ?), it searches in all files that match. The option /S searches in the current directory as well as in its subdirectories. If pattern contains spaces, it must be specified like so /C:"some text to be searched". In order to turn pattern into a regular expressions, the /R option must be used. The /I option searches case insensitive.

From FindStr's Help (Findstr /?):

/R - Uses search strings as regular expressions.

Regular expression quick reference:
  .        Wildcard: any character
  *        Repeat: zero or more occurrences of previous character or class
  ^        Line position: beginning of line
  $        Line position: end of line
  [class]  Character class: any one character in set
  [^class] Inverse class: any one character not in set
  [x-y]    Range: any characters within the specified range
  \x       Escape: literal use of metacharacter x
  \<xyz    Word position: beginning of word
  xyz\>    Word position: end of word
link|improve this answer
Thank you for your answer, but this was not my question. – Aziz Sep 26 '11 at 18:20
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.