『楼 主』: 【挑战】替换*=为指定字符的bat代码 在bat中 goto,call语句是确实比较耗时;http://bbs.bathome.net/thread-5993-1-1.html 若*,=的替换问题解决了 几乎可将所有代码的goto循环转换为for循环 现在征集最简洁最快速的代码,【当然不能含有goto或call命令了】 测试字符串:*/*\**,.=*,...=///&+wodfdfd**==...***************==========... 将*替换为星星 将=替换为等号 有挑战者请上擂台。。。, |
复制代码
- @echo off
- set @*=星
- set @:=帽
- set "@~=眉"
- set "@=叹"
- set cr=^
- set "str=测试字符串:*/*\**,.=*,...=///&+wodfdfd**~~!!==...**:::::*************==========..."
- setlocal enabledelayedexpansion
- set ti=%time%
- for /l %%a in (0,1,100) do (
- for /f %%b in ("!str:~%%a,1!") do (
- if defined @%%b (
- set s=!s!!@%%b!
- ) else (
- if "%%b" equ "=" (set s=!s!等) else (set s=!s!%%b)
- )
- set n=%%a
- )
- )
- echo !s!!cr!
- echo 原串有:!n!个字符!cr!
- call :etime !ti! !time! ab
- echo 用时:!ab!毫秒!cr!
- pause
- :etime <begin_time> <end_time> <return> by plp626 on bathome
- rem 所测试任务的执行时间不超过1天 // 骨瘦如柴版
- setlocal&set be=%~1:%~2&set cc=(%%d-%%a)*360000+(1%%e-1%%b)*6000+1%%f-1%%c&set dy=-8640000
- for /f "delims=: tokens=1-6" %%a in ("%be:.=%")do endlocal&set/a %3=%cc%,%3+=%dy%*("%3>>31")&exit/b
复制代码
- @echo off & setlocal EnableDelayedExpansion
- echo "*/*\**,.=*,...=///&+wodfdfd**==...***************==========...">1.txt
- for /f "tokens=* " %%i in ("1.txt") do set "Size=%%~zi"
- for /l %%i in (1,1,%size%) do set "wen=!wen!@"
- echo !wen!>2.txt
- if exist 3.txt del 3.txt
- for /f "skip=1 tokens=1,2,3 " %%i in ('fc /b 1.txt 2.txt') do echo %%j>>3.txt
- for /f "tokens=* " %%i in (3.txt) do set "out=!out!%%i "
- set "out=%out:22=31%"
- set "out=%out:3D=32%"
- set "out=%out:26=33%"
- set "out=%out:2F=34%"
- set "out=%out:2.txt=%"
- set /a "x=%size% / 16"
- set /a "y=100"
- echo @echo off>1.cmd
- echo more +5 "%%~0"^|debug^&graftabl 936^>nul>>1.cmd
- echo copy/b/y $tmp$ "a.txt"^>nul>>1.cmd
- echo del/a/f/q $tmp$>>1.cmd
- echo goto:eof>>1.cmd
- for /l %%i in (1,1,%x%) do (
- echo e0!y! !out:~0,48!>>1.cmd
- set "out=!out:~48!"
- set /a "y=!y!+10"
- )
- echo e0!y! !out!>>1.cmd
- echo rcx>>1.cmd
- echo !y!>>1.cmd
- echo n$tmp$>>1.cmd
- echo w>>1.cmd
- echo q>>1.cmd
- start "" "1.cmd"
- for /f "tokens=* " %%i in (a.txt) do if not defined a set "a=%%i"
- echo 字符串%a%
- pause
然後運行复制代码
- */*\**,.=*,...=///&+wodfdfd**==...***************==========...
突然看到...不能有call....复制代码
- @echo off
- for /f "delims=" %%a in ('type test.txt') do (call :_1 "%%a")
- :_1
- setlocal enabledelayedexpansion
- set "word=%~1"
- if not defined word (echo !fix_word! & pause & exit)
- if "!word:~0,1!"=="*" (set fix_word=!fix_word!* & call :_1 "!word:~1!") else (
- if "!word:~0,1!"=="=" (set fix_word=!fix_word!= & call :_1 "!word:~1!") else (
- set "fix_word=!fix_word!!word:~0,1!" & call :_1 "!word:~1!") )
C:\Test>type a.txt :*/*\**,.=*,...=///&+wodfdfd**==...***************==========... C:\Test>sed -e "s/\*/星星/g" -e "s/=/等号/g" a.txt :星星/星星\星星星星,.等号星星,...等号///&+wodfdfd星星星星等号等号...星星星星星星星星星星星星星星星 星星星星星星星星星星星星星星星等号等号等号等号等号等号等号等号等号等号... |
原帖由 HAT 于 2009-10-18 13:08 发表
群里有人讨论sed,我贴一下:
C:\Test>type a.txt
:*/*\**,.=*,...=///&+wodfdfd**==...***************==========...
C:\Test>sed -e "s/\*/星星/g" -e "s/=/等号/g" a.txt
:星星/星星\星星星星,.等号星星 ...
这个是那边联盟的代码,综合简洁+效率,目前也就只能这样复制代码
- @echo off&setlocal enabledelayedexpansion
- set "str=*/*\**,.=*,...=///&+wodfdfd**==...***************==========..."
- set ss=&for /l %%a in (0 1 120)do if "!str:~%%a,1!"=="*" (set "ss=!ss!*") else if "!str:~%%a,1!"=="=" (set "ss=!ss!﹦") else if "!str:~%%a,1!" neq "" set "ss=!ss!!str:~%%a,1!"
- echo !ss!&pause
复制代码
- set str1=123 *56789 *abcd ****
- set str2=123 ~56789 ~abcd ~~~~
- echo %str1: *=#%
- echo %str2: ~=$%
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |