标题: [文本处理] 批处理如何在文本中每一行各列的数字/字段内容前面添加指定字符串并按指定格式输出 [打印本页]
作者: laoxin 时间: 2015-12-18 20:52 标题: 批处理如何在文本中每一行各列的数字/字段内容前面添加指定字符串并按指定格式输出
本帖最后由 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
我用以下代码做处理,但是效率很慢:- @Echo Off&Setlocal Enabledelayedexpansion
-
- del b.txt
-
- for /f "tokens=*" %%i in (a.txt) do (
- echo %%i>line_content.txt
-
- for /f "tokens=1 delims=&,#:" %%i in ('findstr "&" line_content.txt') do (
- set string_content=%%i
- )
-
- for /f "tokens=1,2 delims=#" %%i in ('findstr "#" line_content.txt') do (
-
- set cut_bnt_id=%%i
- set cut_bnt=!cut_bnt_id:~0,1!
- if "!cut_bnt!"=="&" (
- set hex_content=%%j
- ) else (set hex_content=%%i)
-
- for %%i in (!hex_content!) do (
- set /a n+=1
- if "!n!"=="1" (
- set cut_hex_id=%%i
- set cut_hex_id1=0x!cut_hex_id:~0,2!
- set cut_hex_id2=,0x!cut_hex_id:~2,2!
- set line_hex=!cut_hex_id1!!cut_hex_id2!
- set /a n+=1
- ) else (set line_hex=!line_hex!,0x%%i)
- )
- echo;!string_content!:lenth=!n!,!line_hex!>>b.txt
- set n=&set line_hex=
- )
-
- )
-
- pause
复制代码
转为以下代码:- @Echo Off&Setlocal Enabledelayedexpansion
-
- del b.txt
-
- for /f "tokens=*" %%i in (a.txt) do (
-
- set line_content=%%i
-
- for /f "tokens=1 delims=&,#:" %%i in ("!line_content!") do (
- set string_content=%%i
- )
-
- for /f "tokens=1,2 delims=#" %%i in ("!line_content!") do (
-
- set cut_bnt_id=%%i
- set cut_bnt=!cut_bnt_id:~0,1!
- if "!cut_bnt!"=="&" (
- set hex_content=%%j
- ) else (set hex_content=%%i)
-
- for %%i in (!hex_content!) do (
- set /a n+=1
- if "!n!"=="1" (
- set cut_hex_id=%%i
- set cut_hex_id1=0x!cut_hex_id:~0,2!
- set cut_hex_id2=,0x!cut_hex_id:~2,2!
- set line_hex=!cut_hex_id1!!cut_hex_id2!
- set /a n+=1
- ) else (set line_hex=!line_hex!,0x%%i)
- )
- echo;!string_content!:lenth=!n!,!line_hex!>>b.txt
- set n=&set line_hex=
- )
-
- )
-
- pause
复制代码
效率是提高了,但是得出的结果完全不对,有哪位大神指点指点遍历字符串和把字符串放到文本中再遍历的区别
作者: laoxin 时间: 2015-12-18 21:00
不好意思,第一个#号后面多了个冒号,但是去掉冒号还是不对
作者: pcl_test 时间: 2015-12-18 21:22
代码1你不是用findstr做了筛选么?那么代码2也加个筛选
if "!line_content:~,1!" equ "&" (
for /f "tokens=1 delims=&#" %%i in ("!line_content!") do (
set string_content=%%i
)
)
作者: laoxin 时间: 2015-12-18 21:37
回复 3# pcl_test
谢谢版主!!管用
作者: WHY 时间: 2015-12-18 22:19
- @if (0)==(0) echo off
- cscript //nologo //e:jscript "%~f0"<a.txt
- pause & exit
- @end
-
- var txt = WSH.StdIn.ReadAll() + '\n&';
- var reg = /&(.+)(#[\S\s]+?)(?=&)/g, arr;
- while(arr = reg.exec(txt)) {
- var a = arr[2].replace(/(\r?\n)+/g, '\n').split('\n');
- for(var i=0, len=a.length-1; i<len; i++) {
- var re = /[a-f\d]{2}/ig, b, n=0, s='';
- while(b = re.exec(a[i])) { n+=1; s+=' 0x' + b[0] };
- WSH.Echo(arr[1] + ':lenth=' + n + '' + s)
- }
- }
复制代码
作者: laoxin 时间: 2015-12-18 22:29
回复 5# WHY
请问这是用什么工具写的,纯批处理不支持while语句吧?
作者: WHY 时间: 2015-12-18 22:45
回复 6# laoxin
你保存为 test.bat 试试呢?
我很好奇,顶楼代码是你自己写的吗?
作者: laoxin 时间: 2015-12-19 00:10
回复 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.
)
作者: terse 时间: 2015-12-19 00:27
仅根据一楼范本写的- @echo off&setlocal enabledelayedexpansion
- (for /f "tokens=*" %%i in (a.txt) do (
- set "str=%%i"
- set n=&set "hex="
- if "!str:~,1!" == "&" (
- for /f "delims=&#" %%i in ("!str!") do set "s=%%i:lenth="
- )
- for %%j in (!str:*#^=!) do (
- set "v=%%j"
- set hex=!hex! 0x!v:~,2!
- if "!v:~2!" neq "" (
- set hex=!hex! 0x!v:~2!
- set /a n+=2
- ) else set /a n+=1
- )
- echo;!s!!n!,!hex:~1!
- ))>b.txt
- pause
复制代码
作者: laoxin 时间: 2015-12-19 00:49
回复 9# terse
非常感谢!!冥思苦想半天,没想到把问题一放上来,马上就有人帮解决了,代码都是以前没见过的,学习了!!
作者: DAIC 时间: 2015-12-23 21:51
回复 6# laoxin
主要是 jscript
用 bat 做了个调用
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |