欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

bat 正则替换

程序员文章站 2022-07-08 16:30:39
...
这是一段javascript,通过bat替换为指定字符串
<script type="text/javascript">
//<![CDATA[
  Socks = 59004;Polymorth = Socks^Socks;BigProxy = 3271^Polymorth;Agile = 35278^33367;SmallBlind = 50597^58473;SmallProxy = 57534^31061;Defender = Socks^62999;GrubMe = Defender^33265;DontGrubMe = 25464^8802;Xorg = 4236^48551;ProxyMoxy = 7348^SmallBlind;DexterProxy = 55550^37552;NineBeforeZero = 4821^16088;BigGoodProxy = BigProxy^SmallBlind;Xinemara = 58604^50313;HttpSocks = NineBeforeZero^50986;BigBlind = 24284^Xorg;
//]]>
</script>


替换结果
set /a   Socks=59004
 set /a Polymorth=%Socks%^%Socks%
 set /a BigProxy=3271^%Polymorth%
 set /a Agile=35278^33367
 set /a SmallBlind=50597^58473
 set /a SmallProxy=57534^31061
 set /a Defender=%Socks%^62999
 set /a GrubMe=%Defender%^33265
 set /a DontGrubMe=25464^8802
 set /a Xorg=4236^48551
 set /a ProxyMoxy=7348^%SmallBlind%
 set /a DexterProxy=55550^37552
 set /a NineBeforeZero=4821^16088
 set /a BigGoodProxy=%BigProxy%^%SmallBlind%
 set /a Xinemara=58604^50313
 set /a HttpSocks=%NineBeforeZero%^50986
 set /a BigBlind=24284^%Xorg%


下载安装sed
http://gnuwin32.sourceforge.net/packages.html
查找sed,下载安装
bat设置环境变量,该目录下有三个.dll文件,如不指定环境变量,会无法执行
set "Path=%Path%;C:\Program Files (x86)\GnuWin32\bin\"



替换脚本

//读入文件本字符串到变量a中
set /p a= < script%%i.cmd

//添加 set /p 字符到开头 并输出到文件
	echo set /a !a! > script%%i.cmd
//将所有的;号替换为 换行 set /a 
	sed -i "s/;/\n set \/a /g" script%%i.cmd 
//删除=号前后的空格
	sed -i "s/ = /=/g" script%%i.cmd 
//将变量用%变量%括起来
 	sed  -ri "s/=([a-zA-Z]*)/=%%\1%%/" script%%i.cmd
 	sed -ri "s/\^([a-zA-Z]*)/\^%%\1%%/" script%%i.cmd

//将多个%%替换为1个%
 	sed -ri "s/%%%%//g" script%%i.cmd
//删除末多余set
 	sed -i "$d" script%%i.cmd
//删除sed开头的临时文件
        del /q sed*




相关标签: 脚本