批处替换那些在内置替换函数句式中有语义的字符如 =~* 是不能用替换函数本身完成的,只能 ‘曲径通幽’,先用最快方法获取字符串长度再用for逐字处理。以下代码会将目标文件中的 * 字符全部删除,亦可删除 = 或 ~ 字符(在以下代码中第8行替换 * 字符即可) ...- @echo off &setlocal
- (for /f "delims=" %%s in (1.txt) do (
- set "s=%%s" &setlocal enabledelayedexpansion &set "_s=!s!" &set "sL=0"
- for %%n in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
- if "!_s:~%%n,1!" neq "" (set/a "sL+=%%n"&set "_s=!_s:~%%n!"))
- set "_s=" &for /l %%n in (0,1,!sL!) do (
- set "c=!s:~,1!" &set "s=!s:~1!"
- if "!c!" neq "" if "!c!" neq "*" set "_s=!_s!!c!")
- echo,!_s!&endlocal
- ))>2.txt
- pause&exit/b
复制代码
当然用 powershell 或 sed.exe 就简捷多了,一行流解决问题...
以下是 powershell 版本- powershell "(gc '1.txt' -raw).replace('*', '')">2.txt
复制代码
以下是 sed.exe 版本(可从本坛第三方下载 sed.exe 落地即用)http://bcn.bathome.net/s/tool/index.html?key=sed- sed "s/\*//gi" 1.txt>2.txt
复制代码