标题: [文本处理] [已解决]批处理如何在小于10(十)/只有一位数的数字前面补零/补0/添加0 [打印本页]
作者: RuiIsRui 时间: 2009-8-2 17:03 标题: [已解决]批处理如何在小于10(十)/只有一位数的数字前面补零/补0/添加0
本帖最后由 pcl_test 于 2016-11-18 21:32 编辑
用批处理将以下字符串中小于10的数子前加上0:
2,15,20,33,56,1,07,54,89,66,23,25,27,28,29,55,56,57,5,59,6,78,74,66,08,28,9,30,52,14,15,16,2,8,4,6,15,16,13,5,9
处理后的结果为:
02,15,20,33,56,01,07,54,89,66,23,25,27,28,29,55,56,57,05,59,06,78,74,66,08,28,09,30,52,14,15,16,02,08,04,06,15,16,13,05,09
处理后次序不能打乱,还是和处理前一样横排,不要竖排,不要分段!
[ 本帖最后由 RuiIsRui 于 2009-8-2 19:18 编辑 ]
作者: tireless 时间: 2009-8-2 17:42
- @echo off & setlocal enabledelayedexpansion
- set var=2,15,20,33,56,1,07,54,89,66,23,25,27,28,29,55,56,57,5,59,6,78,74,66,08,28,9,30,52,14,15,16,2,8,4,6,15,16,13,5,9
- for %%a in (%var%) do (
- if 1%%a lss 20 (set var0=!var0!,0%%a) else set var0=!var0!,%%a
- )
- set var0=%var0:~1%
- echo %var0%
- pause
复制代码
作者: inittab 时间: 2009-8-2 17:58
为了处理方便,请将字串存于a.txt- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%a in (a.txt) do (
- echo 源串:%%a
- for %%b in (%%a) do (
- if 1%%b lss 100 (set var=0%%b) else set var=%%b
- set str=!str!,!var!
- )
- rem 如需要输出,请改为echo;!str:~1!>>file.txt&set str=
- echo;转换:!str:~1!&set str=
- )
- pause
复制代码
作者: Batcher 时间: 2009-8-2 18:16
- sed -i -e "s/^\([1-9]\),/0\1,/g" -e "s/,\([1-9]\),/,0\1,/g" -e "s/,\([1-9]\),/,0\1,/g" -e "s/,\([1-9]\)$/,0\1/g" a.txt
复制代码
第三方命令行工具 sed.exe 下载地址:
http://bbs.bathome.net/thread-1114-1-1.html
作者: RuiIsRui 时间: 2009-8-2 19:17
谢谢2楼 tireless!
作者: netbenton 时间: 2009-8-2 22:20
我也来一个-
- @echo off & setlocal enabledelayedexpansion
- set var=2,15,20,33,56,1,07,54,89,66,23,25,27,28,29,55,56,57,5,59,6,78,74,66,08,28,9,30,52,14,15,16,2,8,4,6,15,16,13,5,9
- set "var=,!var:,=,,!,"
- for /l %%a in (0,1,9) do (set var=!var:,%%a,=,0%%a,!)
- set var=!var:~1,-1!
- set "var=!var:,,=,!"
- echo !var!
- pause
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |