[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理如何在文本中每一行各列的数字/字段内容前面添加指定字符串并按指定格式输出

本帖最后由 pcl_test 于 2016-11-12 15:33 编辑

求助解析批处理直接分割字符串和把字符串导入文本再分割的差异
处理以下文本:
&请求#8022   07 AE 30 06 00 00 00 00
#8042   07 AE 30 06 00 00 00 00
#8042   07 AE 30 06 00 00 00 00
&应答#8122   aa AE 30 04 00 00 00 00
#8122   55 55 30 04 00 00 00 00 33
#8122   55 55 30 04 00 00 00 00 33
&结束#8122   aa AE 30 00 00 00 00 00
&逻辑错误#8122   aa AE 34 0C 00 00 00 00
&请求#8122   aa AE 34 08 00 00 00 00
&链接#8122   aa AE 00 00 00 00 00 00 00 00
达到如下效果:
请求:lenth=10,0x80,0x22,0x07,0xAE,0x30,0x06,0x00,0x00,0x00,0x00
请求:lenth=10,0x80,0x42,0x07,0xAE,0x30,0x06,0x00,0x00,0x00,0x00
请求:lenth=10,0x80,0x42,0x07,0xAE,0x30,0x06,0x00,0x00,0x00,0x00
应答:lenth=10,0x81,0x22,0xaa,0xAE,0x30,0x04,0x00,0x00,0x00,0x00
应答:lenth=11,0x81,0x22,0x55,0x55,0x30,0x04,0x00,0x00,0x00,0x00,0x33
应答:lenth=11,0x81,0x22,0x55,0x55,0x30,0x04,0x00,0x00,0x00,0x00,0x33
结束:lenth=10,0x81,0x22,0xaa,0xAE,0x30,0x00,0x00,0x00,0x00,0x00
逻辑错误:lenth=10,0x81,0x22,0xaa,0xAE,0x34,0x0C,0x00,0x00,0x00,0x00
请求:lenth=10,0x81,0x22,0xaa,0xAE,0x34,0x08,0x00,0x00,0x00,0x00
链接:lenth=12,0x81,0x22,0xaa,0xAE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
我用以下代码做处理,但是效率很慢:
  1. @Echo Off&Setlocal Enabledelayedexpansion
  2. del b.txt
  3. for /f "tokens=*" %%i in (a.txt) do (
  4. echo %%i>line_content.txt
  5. for /f "tokens=1 delims=&,#:" %%i in ('findstr "&" line_content.txt') do (
  6. set string_content=%%i
  7. )
  8. for /f "tokens=1,2 delims=#" %%i in ('findstr "#" line_content.txt') do (
  9. set cut_bnt_id=%%i
  10. set cut_bnt=!cut_bnt_id:~0,1!
  11.   if "!cut_bnt!"=="&" (
  12.    set hex_content=%%j  
  13.   ) else (set hex_content=%%i)
  14. for %%i in (!hex_content!) do (
  15. set /a n+=1
  16. if "!n!"=="1" (
  17. set cut_hex_id=%%i
  18. set cut_hex_id1=0x!cut_hex_id:~0,2!
  19. set cut_hex_id2=,0x!cut_hex_id:~2,2!
  20. set line_hex=!cut_hex_id1!!cut_hex_id2!
  21. set /a n+=1
  22. ) else (set line_hex=!line_hex!,0x%%i)
  23. )
  24. echo;!string_content!:lenth=!n!,!line_hex!>>b.txt
  25. set n=&set line_hex=
  26. )
  27. )
  28. pause
复制代码
转为以下代码:
  1. @Echo Off&Setlocal Enabledelayedexpansion
  2. del b.txt
  3. for /f "tokens=*" %%i in (a.txt) do (
  4. set line_content=%%i
  5. for /f "tokens=1 delims=&,#:" %%i in ("!line_content!") do (
  6. set string_content=%%i
  7. )
  8. for /f "tokens=1,2 delims=#" %%i in ("!line_content!") do (
  9. set cut_bnt_id=%%i
  10. set cut_bnt=!cut_bnt_id:~0,1!
  11.   if "!cut_bnt!"=="&" (
  12.    set hex_content=%%j  
  13.   ) else (set hex_content=%%i)
  14. for %%i in (!hex_content!) do (
  15. set /a n+=1
  16. if "!n!"=="1" (
  17. set cut_hex_id=%%i
  18. set cut_hex_id1=0x!cut_hex_id:~0,2!
  19. set cut_hex_id2=,0x!cut_hex_id:~2,2!
  20. set line_hex=!cut_hex_id1!!cut_hex_id2!
  21. set /a n+=1
  22. ) else (set line_hex=!line_hex!,0x%%i)
  23. )
  24. echo;!string_content!:lenth=!n!,!line_hex!>>b.txt
  25. set n=&set line_hex=
  26. )
  27. )
  28. pause
复制代码
效率是提高了,但是得出的结果完全不对,有哪位大神指点指点遍历字符串和把字符串放到文本中再遍历的区别

不好意思,第一个#号后面多了个冒号,但是去掉冒号还是不对

TOP

代码1你不是用findstr做了筛选么?那么代码2也加个筛选
if "!line_content:~,1!" equ "&" (
                for /f "tokens=1 delims=&#" %%i in ("!line_content!") do (
                        set string_content=%%i
                )
)
1

评分人数

TOP

回复 3# pcl_test


    谢谢版主!!管用

TOP

  1. @if (0)==(0) echo off
  2. cscript //nologo //e:jscript "%~f0"<a.txt
  3. pause & exit
  4. @end
  5. var txt = WSH.StdIn.ReadAll() + '\n&';
  6. var reg = /&(.+)(#[\S\s]+?)(?=&)/g, arr;
  7. while(arr = reg.exec(txt)) {
  8.     var a = arr[2].replace(/(\r?\n)+/g, '\n').split('\n');
  9.     for(var i=0, len=a.length-1; i<len; i++) {
  10.         var re = /[a-f\d]{2}/ig, b, n=0, s='';
  11.         while(b = re.exec(a[i])) { n+=1; s+=' 0x' + b[0] };
  12.         WSH.Echo(arr[1] + ':lenth=' + n + '' + s)
  13.     }
  14. }
复制代码
1

评分人数

TOP

回复 5# WHY


    请问这是用什么工具写的,纯批处理不支持while语句吧?

TOP

回复 6# laoxin


    你保存为 test.bat 试试呢?
我很好奇,顶楼代码是你自己写的吗?

TOP

回复 7# WHY


    哇哦,还真管用,拜谢!!代码太经典了,我才刚接触批处理一个礼拜,看网上的资料都没有while语句的用法,顶楼的代码是根据本论坛-->'回家的路上‘提供的改写的:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims=:" %%a in (result.txt) do (
  for %%b in (%%a) do (
    set str=%%b
    set str=0x!str!
    set /p =!str!,<nul
  )
  echo.
)

TOP

仅根据一楼范本写的
  1. @echo off&setlocal enabledelayedexpansion
  2. (for /f "tokens=*" %%i in (a.txt) do (
  3.     set "str=%%i"
  4.     set n=&set "hex="
  5.     if "!str:~,1!" == "&" (
  6.        for /f "delims=&#" %%i in ("!str!") do set "s=%%i:lenth="
  7.     )
  8.     for %%j in (!str:*#^=!) do (
  9.         set "v=%%j"
  10.         set hex=!hex! 0x!v:~,2!
  11.         if "!v:~2!" neq "" (
  12.            set hex=!hex! 0x!v:~2!
  13.            set /a n+=2
  14.         ) else set /a n+=1
  15.     )
  16.     echo;!s!!n!,!hex:~1!
  17. ))>b.txt
  18. pause
复制代码
1

评分人数

TOP

回复 9# terse


    非常感谢!!冥思苦想半天,没想到把问题一放上来,马上就有人帮解决了,代码都是以前没见过的,学习了!!

TOP

回复 6# laoxin


主要是 jscript
用 bat 做了个调用

TOP

返回列表