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

[文本处理] 求助批处理怎么分割一行数据?

{1., 352400.11}, {9., 348500.91}, {15., 349000.76}, {21., 348800.83}, {29., 330900.75}, {37., 327100.03}, {45., 328300.43}, {49., 321300.33}, {51., 322500.64}, {62., 306700.}, {74., 314600.}
以上一行数据怎么分割成以下模式二个数据啊?谢谢
数据1
1 -> 1, 2 -> 9, 3 -> 15, 4 -> 21, 5 -> 29, 6 -> 37, 7 -> 45, 8 -> 49, 9 -> 51, 10 -> 62, 11 -> 74
数据二
1 -> 352400, 2 -> 348500, 3 -> 349000, 4 -> 348800, 5 -> 330900, 6 -> 327100, 7 -> 328300, 8 -> 321300, 9 -> 322500, 10 -> 306700, 11 -> 314600

谢谢您
热心网友,明天测试下

TOP

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "str={1., 352400.11}, {9., 348500.91}, {15., 349000.76}, {21., 348800.83}, {29., 330900.75}, {37., 327100.03}, {45., 328300.43}, {49., 321300.33}, {51., 322500.64}, {62., 306700.}, {74., 314600.}"
  4. set str=%str:{=%
  5. set str=%str:}=%
  6. set /a l=m=n=1
  7. for %%a in (%str%) do (
  8.   set /a odd=l%%2
  9.   set /a l+=1
  10.   for /f "delims=." %%i in ("%%a") do (
  11.     if !odd!==1 (
  12.       set "line1=!line1! !m! -> %%i,"
  13.       set /a m+=1
  14.     ) else (
  15.       set "line2=!line2! !n! -> %%i,"
  16.       set /a n+=1
  17.     )
  18.   )
  19. )
  20. echo;!line1!
  21. echo;!line2!
  22. pause>nul
复制代码

TOP

给一个纯P代码实现方案...
@echo off & setlocal enabledelayedexpansion
set "nList={1., 352400.11}, {9., 348500.91}, {15., 349000.76}, {21., 348800.83}, {29., 330900.75}, {37., 327100.03}, {45., 328300.43}, {49., 321300.33}, {51., 322500.64}, {62., 306700.}, {74., 314600.}"
set items=!nList:},=}^

!
:: 上面两个空行不能被省略
set/a n=1
for /f "tokens=1-2 delims={., " %%1 in ("!items!") do (
    set "data1=!data1!!n! -> %%1, "
    set "data2=!data2!!n! -> %%2, "
    set/a n+=1
)
echo,!data1:~,-2!
echo,!data2:~,-2!
endlocal&pause&exit/b
1

评分人数

TOP

  1. #&cls&powershell "iex(gc '%~0'|out-string)"&exit\b
  2. $a="{1., 352400.11}, {9., 348500.91}, {15., 349000.76}, {21., 348800.83}, {29., 330900.75}, {37., 327100.03}, {45., 328300.43}, {49., 321300.33}, {51., 322500.64}, {62., 306700.}, {74., 314600.}"
  3. $one=[regex]::matches($a,'(?<=\{)\d+(?=\.)').value
  4. $two=[regex]::matches($a,'(?<=\s)\d+(?=\.)').value
  5. (1..$one.count|%{"$_->"+$one[$_-1]}) -join ","
  6. (1..$two.count|%{"$_->"+$two[$_-1]}) -join ","
  7. pause
复制代码
输出如下:
1->1,2->9,3->15,4->21,5->29,6->37,7->45,8->49,9->51,10->62,11->74
1->352400,2->348500,3->349000,4->348800,5->330900,6->327100,7->328300,8->321300,9->322500,10->306700,11->314600
按 Enter 键继续...:

TOP

JS脚本
  1. //分割数据.js//操作:拖入数据文本文件
  2. //有多项,每项用{}括起,项间逗号分隔。每项中有两个数据,数据间也是逗号分隔。
  3. DataFile=WScript.Arguments(0);
  4. Stream = new ActiveXObject("ADODB.Stream"); //打开文件,逐个读取字符并判断
  5. Stream.Type=2; //类型1是二进制,2是文本
  6. Stream.Charset = "GBK";
  7. Stream.Open;
  8. Stream.LoadFromFile(DataFile);
  9. var tstr='',iflag=n=0;
  10. var first=[],second=[]; //定义数组,用于存放两组数值
  11. while(!Stream.EOS){
  12. Rchar=Stream.ReadText(1);
  13. tstr+=Rchar;
  14. if(iflag){
  15. if(Rchar=='}'){ //结束一条,同时是第二个数据
  16. iflag=0;second[n]=parseInt(tstr); //parseInt(tstr)会去除末尾非数字部分
  17. }else if(Rchar==','){ //{第一个数据/逗号}
  18. first[n]=parseInt(tstr);tstr='';
  19. }
  20. }else if(Rchar=='{'){ //开始一条
  21. iflag=1;tstr='';n++;
  22. }
  23. }
  24. Stream.Close;
  25. group1=group2='';
  26. for(i=1;i<n;i++){
  27. group1+=i+' -> '+first[i]+', '; //一组:1 -> 1, 2 -> 9,
  28. group2+=i+' -> '+second[i]+', '; //二组:1 -> 352400, 2 -> 348500,
  29. }
  30. group1+=n+' -> '+first[n];
  31. group2+=n+' -> '+second[n];
  32. WSH.Echo('数据一\n'+group1+'\n\n数据二\n'+group2);
复制代码
如无特别说明,代码测试环境均为 XP SP3

TOP

抛个砖:
  1. my $raw = '{1., 352400.11}, {9., 348500.91}, {15., 349000.76}, {21., 348800.83}, {29., 330900.75}, {37., 327100.03}, {45., 328300.43}, {49., 321300.33}, {51., 322500.64}, {62., 306700.}, {74., 314600.}';
  2. my @d = eval "($raw)"=~tr/{}/[]/r;
  3. printf "data 1: %s\n", join( ", ", map { sprintf "%d -> %d", $_+1, $d[$_][0] } ( 0 .. $#d) );
  4. printf "data 2: %s\n", join( ", ", map { sprintf "%d -> %d", $_+1, $d[$_][1] } ( 0 .. $#d) );
复制代码
脚本语言: perl

输出
data 1: 1 -> 1, 2 -> 9, 3 -> 15, 4 -> 21, 5 -> 29, 6 -> 37, 7 -> 45, 8 -> 49, 9 -> 51, 10 -> 62, 11 -> 74
data 2: 1 -> 352400, 2 -> 348500, 3 -> 349000, 4 -> 348800, 5 -> 330900, 6 -> 327100, 7 -> 328300, 8 -> 321300, 9 -> 322500, 10 -> 306700, 11 -> 314600

TOP

返回列表