|  | 
 
| wydos 中级用户
 
    
 
 
 
 积分 304
 发帖 117
 注册 2006-4-4
 状态 离线
 | 
| 『楼 主』:
 [求助]怎样用bat对齐文本?
 
使用 LLM 解释/回答一下 
 
 
文本内容形式是这样的:新编中医临证备要 作者:卢祥之  SS号:10804820   索书号:R24/15
 中医临床学基础 作者:湖南中医学院  SS号:10806223   索书号:R24/1
 临床证治精论 作者:徐春家 秦群   SS号:10806254   索书号:R24/152
 .
 .
 .
 我想把它按类对齐,变成如下形式:
 新编中医临证备要 作者:卢祥之          SS号:10804820   索书号:24/15
 中医临床学基础    作者:湖南中医学院   SS号:10806223   索书号:24/1
 临床证治精论       作者:徐春家 秦群   SS号:10806254   索书号:24/152
 .
 .
 .
 .
 .
 
 Last edited by wydos on 2006-12-13 at 07:17 AM ]
 
New Compilation of Essentials for TCM Clinical Practice  Author: Lu Xiangzhi  SS Number: 10804820  Call Number: R24/15
 
 Basic Theory of TCM Clinical Medicine
 Author: Hunan College of TCM  SS Number: 10806223  Call Number: R24/1
 
 Essential Discussions on Clinical Syndrome Treatment
 Author: Xu Chunjia, Qin Qun  SS Number: 10806254  Call Number: R24/152
 
 Last edited by wydos on 2006-12-13 at 07:17 AM ]
 
 
 
 |  | 
|  2006-12-13 07:15 |  | 
|  | 
 
| a9319751 中级用户
 
    
 
 
 
 积分 439
 发帖 170
 注册 2006-1-9
 状态 离线
 | 
| 『第 2 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
隐隐约约记得哪个能制造TAB跳格好象是8个空格
 
Vaguely remember that there is one that can make TAB tab. It seems to be 8 spaces. 
 
 
 
 |  
                  |  bat c c++
 |  | 
|  2006-12-13 07:19 |  | 
|  | 
 
| namejm 荣誉版主
 
        batch fan
 
 
 积分 5226
 发帖 1737
 注册 2006-3-10
 来自 成都
 状态 离线
 | 
| 『第 3 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
  需要统计每一节字符串的最大长度才好排版。 
We need to count the maximum length of each section of the string to facilitate typesetting. 
 
 
 
 |  
                  |  尺有所短,寸有所长,学好CMD没商量。
 考虑问题复杂化,解决问题简洁化。
 |  | 
|  2006-12-13 07:35 |  | 
|  | 
 
| lxmxn 版主
 
         
 
 
 积分 11386
 发帖 4938
 注册 2006-7-23
 状态 离线
 | 
| 『第 4 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
不知道WMIC可不可以,问下版主就知道了。
 
I don't know if WMIC can do it. Just ask the moderator and you'll know.
 
 
 
 |  | 
|  2006-12-13 07:53 |  | 
|  | 
 
| youxi01 高级用户
 
     
 
 
 积分 846
 发帖 247
 注册 2006-10-27
 来自 湖南==》广东
 状态 离线
 | 
|   『第 5 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
个人认为,纯粹的P很难实现,因为P很难判断变量的字节数,所以要借助于vbs。 ::On Error Resume Next
 echo ' > nul &cls & @echo off
 echo ' > nul &for /f "tokens=1,2,3,4 delims= " %%i in (test.txt) do (
 echo ' > nul &cscript //E:vbs "%~f0"  "%%i" "%%j" "%%k" "%%l" //nologo)
 echo ' > nul &pause>nul
 
 ::str0=wscript.arguments(0)
 ::str1=wscript.arguments(1)
 ::str2=wscript.arguments(2)
 ::str3=wscript.arguments(3)
 ::wscript.echo align(str0)&align(str1)&align(str2)&align(str3)
 ::function align(obj)
 ::  length=len(obj)
 ::  flag=0
 ::  for i=1 to length
 ::     str=mid(obj,i,1)
 ::     str=asc(str)
 ::     if str<0 then
 ::         flag=flag+1
 ::     end if
 ::   next
 ::   length=20-length-flag
 ::   align=obj&space(length)
 ::end function
 
 
说明:将要处理的文字保存为test.txt,本代码保存为:ok.bat
 
 Last edited by youxi01 on 2006-12-13 at 08:14 AM ]
Personally, purely using P is difficult to implement because it's hard to judge the number of bytes of variables, so we need to rely on vbs. ::On Error Resume Next
 echo ' > nul &cls & @echo off
 echo ' > nul &for /f "tokens=1,2,3,4 delims= " %%i in (test.txt) do (
 echo ' > nul &cscript //E:vbs "%~f0"  "%%i" "%%j" "%%k" "%%l" //nologo)
 echo ' > nul &pause>nul
 
 ::str0=wscript.arguments(0)
 ::str1=wscript.arguments(1)
 ::str2=wscript.arguments(2)
 ::str3=wscript.arguments(3)
 ::wscript.echo align(str0)&align(str1)&align(str2)&align(str3)
 ::function align(obj)
 ::  length=len(obj)
 ::  flag=0
 ::  for i=1 to length
 ::     str=mid(obj,i,1)
 ::     str=asc(str)
 ::     if str<0 then
 ::         flag=flag+1
 ::     end if
 ::   next
 ::   length=20-length-flag
 ::   align=obj&space(length)
 ::end function
 
 
Description: Save the text to be processed as test.txt, and save this code as: ok.bat
 
 Last edited by youxi01 on 2006-12-13 at 08:14 AM ]
 
 
 
 
 |  | 
|  2006-12-13 08:03 |  | 
|  | 
 
| electronixtar 铂金会员
 
        
 
 
 
 积分 7493
 发帖 2672
 注册 2005-9-2
 状态 离线
 | 
| 『第 6 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
不知道WMIC可不可以,问下版主就知道了。
 
 
汗,WMIC拿来处理文本~~~ 
I don't know if WMIC can do it. Just ask the moderator and you'll know.
 
 
Sweat, WMIC is used to handle text~~~ 
 
 
 
 |  
                  |  
 C:\>BLOG http://initiative.yo2.cn/
 C:\>hh.exe ntcmds.chm::/ntcmds.htm
 C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
 |  | 
|  2006-12-13 09:15 |  | 
|  | 
 
| pengfei 银牌会员
 
      
 
 
 积分 1218
 发帖 485
 注册 2006-7-21
 来自 湖南.娄底
 状态 离线
 | 
|  『第 7 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
@echo off
 setlocal enabledelayedexpansion
 for /f "tokens=1-4 delims=作者:SS号:索书号:" %%i in (test.txt) do (
 set a=&set b=&set c=
 call :detect %%i
 set /a num=2*10-2*num
 call :add %%i
 set a=!i!
 call :detect %%j
 set /a num=2*8-2*num
 call :spa %%j
 set /a num+=!sp!
 call :add %%j
 set b=!i!
 call :detect %%k
 set /a num=12-num
 call :add %%k
 set c=!i!
 echo !a!作者:!b!SS号:!c!索书号:%%l>>array.txt
 )
 start array.txt
 goto :eof
 
 :detect
 set x=%*◎
 set num=0
 :detect_
 if not "%x:~0,1%"=="◎" (
 set /a num+=1
 set "x=%x:~1%"
 goto :detect_
 )
 goto :eof
 
 :add
 set y=%*
 for /l %%i in (1,1,%num%) do set y=!y!
 set i=%y%
 goto :eof
 
 :spa
 set z=%*◎
 set sp=0
 :spa_
 if not "%z:~0,1%"=="◎" (
 if "%z:~0,1%"==" " set /a sp+=1
 set "z=%z:~1%"
 goto :spa_
 )
 goto :eof
 
 
只测试了你给的三行, 其他格式和特殊字符没有考虑. 请注意冒号全角和半角的区别.
 
 Last edited by pengfei on 2006-12-13 at 12:53 PM ] 
```@echo off
 setlocal enabledelayedexpansion
 for /f "tokens=1-4 delims=Author: SS number: Call number: " %%i in (test.txt) do (
 set a=&set b=&set c=
 call :detect %%i
 set /a num=2*10-2*num
 call :add %%i
 set a=!i!
 call :detect %%j
 set /a num=2*8-2*num
 call :spa %%j
 set /a num+=!sp!
 call :add %%j
 set b=!i!
 call :detect %%k
 set /a num=12-num
 call :add %%k
 set c=!i!
 echo !a!Author: !b!SS number: !c!Call number: %%l>>array.txt
 )
 start array.txt
 goto :eof
 
 :detect
 set x=%*◎
 set num=0
 :detect_
 if not "%x:~0,1%"=="◎" (
 set /a num+=1
 set "x=%x:~1%"
 goto :detect_
 )
 goto :eof
 
 :add
 set y=%*
 for /l %%i in (1,1,%num%) do set y=!y!
 set i=%y%
 goto :eof
 
 :spa
 set z=%*◎
 set sp=0
 :spa_
 if not "%z:~0,1%"=="◎" (
 if "%z:~0,1%"==" " set /a sp+=1
 set "z=%z:~1%"
 goto :spa_
 )
 goto :eof
 ```
 
 Only tested the three lines you provided, and other formats and special characters were not considered. Please note the difference between full-width and half-width colons.
 
 Last edited by pengfei on 2006-12-13 at 12:53 PM ]
 
 
 | 此帖被 +9 点积分  点击查看详情 | 评分人:【 无奈何 】 | 分数: +4 | 时间:2006-12-14 02:33 |  | 评分人:【 redtek 】 | 分数: +5 | 时间:2006-12-14 21:35 | 
 | 
 
 
 
 
 |  
                  |  业精于勤而荒于嬉,形成于思而毁于随。
 |  | 
|  2006-12-13 12:09 |  | 
|  | 
 
| zh159 金牌会员
 
       
 
 
 积分 3687
 发帖 1467
 注册 2005-8-8
 状态 离线
 | 
|   『第 8 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
“delims=作者:SS号:索书号:”中有个缺点,只要某个位置有其中一种字符就导致缺失 
可以先考虑将“作者:”“SS号:”“索书号:”替换为$后再处理
 
亮点:检测字节数
 >Temp.txt set/p=!Temp_%1!<nulfor %%i in (Temp.txt) do set %1=%%~zi
 @echo off
 setlocal enabledelayedexpansion
 cd.>array.txt
 ::检测每段最大字节数
 for /f "delims=" %%i in (test.txt) do (
 set "Temp$=%%i"
 set "Temp$=!Temp$:作者:=$!"&&set "Temp$=!Temp$:SS号:=$!"&&set "Temp$=!Temp$:索书号:=$!"
 for /f "tokens=1-4 delims=$" %%i in ("!Temp$!") do (
 set "Temp_=%%i"&&call :MaxNum 书名
 set "Temp_=%%j"&&call :MaxNum 作者
 set "Temp_=%%k"&&call :MaxNum SS号
 set "Temp_=%%l"&&call :MaxNum 索书号
 ))
 
 ::加上空格对齐
 for /f "delims=" %%i in (test.txt) do (
 set "Temp$=%%i"
 set "Temp$=!Temp$:作者:=$!"&&set "Temp$=!Temp$:SS号:=$!"&&set "Temp$=!Temp$:索书号:=$!"
 for /f "tokens=1-4 delims=$" %%i in ("!Temp$!") do (
 
 set "Temp_书名=%%i"&&call :Num 书名
 set /a 书名=%Max书名%-!书名!&&call :Add 书名
 
 set "Temp_作者=%%j"&&call :Num 作者
 set /a 作者=%Max作者%-!作者!&&call :Add 作者
 
 set "Temp_SS号=%%k"&&call :Num SS号
 set /a SS号=%MaxSS号%-!SS号!&&call :Add SS号
 
 set "Temp_索书号=%%l"&&call :Num 索书号
 set /a 索书号=%Max索书号%-!索书号!&&call :Add 索书号
 
 >>array.txt echo.!Temp_书名!作者:!Temp_作者!SS号:!Temp_SS号!索书号:!Temp_索书号!
 ))
 del Temp.txt
 pause
 exit
 
 ::检测字节数
 :MaxNum
 >Temp.txt set/p=%Temp_%<nul
 for %%i in (Temp.txt) do if %%~zi GTR !Max%1! set Max%1=%%~zi
 goto :eof
 
 ::检测字节数
 :Num
 >Temp.txt set/p=!Temp_%1!<nul
 for %%i in (Temp.txt) do set %1=%%~zi
 goto :eof
 
 ::加空格数量
 :Add
 if "!%1!" == "0" goto :eof
 set "Temp_%1=!Temp_%1! "
 set /a %1-=1
 goto Add
 
 
 Last edited by zh159 on 2006-12-13 at 09:35 AM ]
“delims=Author: SS number: Call number:” has a shortcoming. As long as there is one of these characters in a certain position, it will cause a loss. 
You can first consider replacing "Author:", "SS number:", "Call number:" with $ and then handle it.
 
Highlight: Detect byte count
 >Temp.txt set/p=!Temp_%1!<nulfor %%i in (Temp.txt) do set %1=%%~zi
 @echo off
 setlocal enabledelayedexpansion
 cd.>array.txt
 ::Detect the maximum number of bytes in each segment
 for /f "delims=" %%i in (test.txt) do (
 set "Temp$=%%i"
 set "Temp$=!Temp$:Author:=$!"&&set "Temp$=!Temp$:SS number:=$!"&&set "Temp$=!Temp$:Call number:=$!"
 for /f "tokens=1-4 delims=$" %%i in ("!Temp$!") do (
 set "Temp_=%%i"&&call :MaxNum Book title
 set "Temp_=%%j"&&call :MaxNum Author
 set "Temp_=%%k"&&call :MaxNum SS number
 set "Temp_=%%l"&&call :MaxNum Call number
 ))
 
 ::Add spaces for alignment
 for /f "delims=" %%i in (test.txt) do (
 set "Temp$=%%i"
 set "Temp$=!Temp$:Author:=$!"&&set "Temp$=!Temp$:SS number:=$!"&&set "Temp$=!Temp$:Call number:=$!"
 for /f "tokens=1-4 delims=$" %%i in ("!Temp$!") do (
 
 set "Temp_Book title=%%i"&&call :Num Book title
 set /a Book title=%MaxBook title%-!Book title!&&call :Add Book title
 
 set "Temp_Author=%%j"&&call :Num Author
 set /a Author=%MaxAuthor%-!Author!&&call :Add Author
 
 set "Temp_SS number=%%k"&&call :Num SS number
 set /a SS number=%MaxSS number%-!SS number!&&call :Add SS number
 
 set "Temp_Call number=%%l"&&call :Num Call number
 set /a Call number=%MaxCall number%-!Call number!&&call :Add Call number
 
 >>array.txt echo.!Temp_Book title!Author:!Temp_Author!SS number:!Temp_SS number!Call number:!Temp_Call number!
 ))
 del Temp.txt
 pause
 exit
 
 ::Detect byte count
 :MaxNum
 >Temp.txt set/p=%Temp_%<nul
 for %%i in (Temp.txt) do if %%~zi GTR !Max%1! set Max%1=%%~zi
 goto :eof
 
 ::Detect byte count
 :Num
 >Temp.txt set/p=!Temp_%1!<nul
 for %%i in (Temp.txt) do set %1=%%~zi
 goto :eof
 
 ::Add number of spaces
 :Add
 if "!%1!" == "0" goto :eof
 set "Temp_%1=!Temp_%1! "
 set /a %1-=1
 goto Add
 
 
 Last edited by zh159 on 2006-12-13 at 09:35 AM ]
 
 | 此帖被 +26 点积分    点击查看详情 | 评分人:【 无奈何 】 | 分数: +8 | 时间:2006-12-14 02:33 |  | 评分人:【 youxi01 】 | 分数: +4 | 时间:2006-12-14 03:06 |  | 评分人:【 pengfei 】 | 分数: +5 | 时间:2006-12-14 04:30 |  | 评分人:【 kcdsw 】 | 分数: +4 | 时间:2006-12-14 06:39 |  | 评分人:【 redtek 】 | 分数: +5 | 时间:2006-12-14 21:35 | 
 | 
 
 
 
 |  | 
|  2006-12-13 14:30 |  | 
|  | 
 
| HUNRYBECKY 银牌会员
 
      
 
 
 
 积分 1179
 发帖 442
 注册 2006-9-9
 状态 离线
 | 
| 『第 9 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
都太棒了,我对FOR语句总是用的不好。想不到FOR语句用处这么大,学习了。 
It's all great. I've always been not good at using the FOR statement. I didn't expect the FOR statement to be so useful. Learned it. 
 
 
 |  | 
|  2006-12-13 21:53 |  | 
|  | 
 
| HUNRYBECKY 银牌会员
 
      
 
 
 
 积分 1179
 发帖 442
 注册 2006-9-9
 状态 离线
 | 
| 『第 10 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
Originally posted by youxi01 at 2006-12-13 08:03:个人认为,纯粹的P很难实现,因为P很难判断变量的字节数,所以要借助于vbs。
 
 ::On Error Resume Next
 echo ' > nul &cls & @echo off
 echo ' > nul & ...
 
问下,::不是和REM 一样,是注释吗?为什么把他的::后,运行就不正常了.奇怪,楼主能否解释下. 
Originally posted by youxi01 at 2006-12-13 08:03:In my opinion, pure P is difficult to implement because P is difficult to judge the number of bytes of variables, so it is necessary to rely on vbs.
 
 ::On Error Resume Next
 echo ' > nul &cls & @echo off
 echo ' > nul & ...
 
Ask, :: is the same as REM, which is a comment? Why is the operation abnormal after removing ::? Strange, can the building owner explain it. 
 
 
 |  | 
|  2006-12-13 22:14 |  | 
|  | 
 
| youxi01 高级用户
 
     
 
 
 积分 846
 发帖 247
 注册 2006-10-27
 来自 湖南==》广东
 状态 离线
 | 
| 『第 11 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
因为::在vbs中可以被认识(识别为换行),而rem却不能被vbs认识! 
Because :: can be recognized (identified as a line break) in VBS, while rem cannot be recognized by VBS! 
 
 
 
 
 |  | 
|  2006-12-14 01:18 |  | 
|  | 
 
| youxi01 高级用户
 
     
 
 
 积分 846
 发帖 247
 注册 2006-10-27
 来自 湖南==》广东
 状态 离线
 | 
| 『第 12 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
7F的问题:要是书名中有E文字符或者数字,怎么解决?8F用文件的大小来判断字节数,真是高!
 
Question from user 7F: If there are E characters or numbers in the book title, how to solve it?What an excellent idea by user 8F to judge the number of bytes by the size of the file!
 
 
 
 |  | 
|  2006-12-14 01:22 |  | 
|  | 
 
| qzwqzw 银牌会员
 
      天的白色影子
 
 
 积分 2343
 发帖 636
 注册 2004-3-6
 状态 离线
 | 
| 『第 13 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
对齐还是先补后拆最省事。 
Aligning or first supplementing and then splitting is the most convenient. 
 
 
 |  | 
|  2006-12-14 01:51 |  | 
|  | 
 
| 无奈何 荣誉版主
 
        
 
 
 
 积分 1338
 发帖 356
 注册 2005-7-15
 状态 离线
 | 
|   『第 14 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
zh159 兄 8 楼用字节数判长确是亮点,CMD 下字串汉字和英文字母长度都是 1 ,用字节判断很好的解决了这个问题。不过 CMD 下特殊字符实在是然人头疼的问题,我也一直没有找到好的方法。 
本帖是很典型的格式化输出的问题,大多数语言 pringf 都能很好解决。 
可以试试下面的 awk 的脚本: 
用法:gawk -f script.awk file.txt
 BEGIN{
 FS=" +(作者|号|索书号):"
 }
 {
 L1=L1<length($1)?(length($1)):L1
 L2=L2<length($2)?(length($2)):L2
 L3=L3<length($3)?(length($3)):L3
 L4=L4<length($4)?(length($4)):L4
 for (i=1;i<=NF;i++)
 R=$i
 }
 END{
 format = "%-"L1"s 作者:%-"L2"s SS号:%-"L3"s 索书号:%-"L4"s\n"
 for(i=1;i<=NR;i++)
 if(R != "") printf format, R,R,R,R
 }
 
 
gawk 下载:http://www.klabaster.com/progs/gawk32.zip
 
 Last edited by 无奈何 on 2006-12-14 at 03:23 AM ]
Brother zh159, the use of byte count to determine length in building 8 is indeed a highlight. In CMD, both Chinese characters and English letters in a string have a length of 1, and using byte judgment well solves this problem. However, special characters in CMD are really a headache, and I have not found a good method all the time. 
This post is a very typical problem of formatted output. Most languages' printf can solve it well.
 
You can try the following awk script:
 
Usage: gawk -f script.awk file.txt
 BEGIN{
 FS=" +(作者|号|索书号):"
 }
 {
 L1=L1<length($1)?(length($1)):L1
 L2=L2<length($2)?(length($2)):L2
 L3=L3<length($3)?(length($3)):L3
 L4=L4<length($4)?(length($4)):L4
 for (i=1;i<=NF;i++)
 R=$i
 }
 END{
 format = "%-"L1"s 作者:%-"L2"s SS号:%-"L3"s 索书号:%-"L4"s\n"
 for(i=1;i<=NR;i++)
 if(R != "") printf format, R,R,R,R
 }
 
 
gawk download: http://www.klabaster.com/progs/gawk32.zip
 
 Last edited by 无奈何 on 2006-12-14 at 03:23 AM ]
 
 
 
 
 
 |  
                  |  ☆开始\运行 (WIN+R)☆
 %ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
 
 
 |  | 
|  2006-12-14 02:31 |  | 
|  | 
 
| ccwan 金牌会员
 
       
 
 
 积分 2725
 发帖 1160
 注册 2006-9-23
 来自 河北廊坊
 状态 离线
 | 
| 『第 15 楼』:
 
 
使用 LLM 解释/回答一下 
 
 
恕我愚钝,兄的代码看不懂。为防拍马的嫌疑,就不给兄加分了,见谅。
 
I'm sorry I'm slow-witted, I can't understand brother's code.To avoid the suspicion of flattery, I won't give brother points, please understand.
 
 
 
 
 |  
                  |  三人行,必有吾师焉。   学然后知不足,教然后知困,然后能自强也。
 |  | 
|  2006-12-14 02:44 |  |