标题: [文本处理] 求助批处理怎么分割一行数据? [打印本页]
作者: JK_m 时间: 2022-5-22 19:46 标题: 求助批处理怎么分割一行数据?
{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
作者: 523066680 时间: 2022-5-22 20:17
抛个砖:- 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.}';
- my @d = eval "($raw)"=~tr/{}/[]/r;
- printf "data 1: %s\n", join( ", ", map { sprintf "%d -> %d", $_+1, $d[$_][0] } ( 0 .. $#d) );
- 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
作者: cutebe 时间: 2022-5-23 10:03
JS脚本- //分割数据.js//操作:拖入数据文本文件
- //有多项,每项用{}括起,项间逗号分隔。每项中有两个数据,数据间也是逗号分隔。
- DataFile=WScript.Arguments(0);
-
- Stream = new ActiveXObject("ADODB.Stream"); //打开文件,逐个读取字符并判断
- Stream.Type=2; //类型1是二进制,2是文本
- Stream.Charset = "GBK";
- Stream.Open;
- Stream.LoadFromFile(DataFile);
-
- var tstr='',iflag=n=0;
- var first=[],second=[]; //定义数组,用于存放两组数值
- while(!Stream.EOS){
- Rchar=Stream.ReadText(1);
- tstr+=Rchar;
- if(iflag){
- if(Rchar=='}'){ //结束一条,同时是第二个数据
- iflag=0;second[n]=parseInt(tstr); //parseInt(tstr)会去除末尾非数字部分
- }else if(Rchar==','){ //{第一个数据/逗号}
- first[n]=parseInt(tstr);tstr='';
- }
- }else if(Rchar=='{'){ //开始一条
- iflag=1;tstr='';n++;
- }
- }
- Stream.Close;
-
- group1=group2='';
- for(i=1;i<n;i++){
- group1+=i+' -> '+first[i]+', '; //一组:1 -> 1, 2 -> 9,
- group2+=i+' -> '+second[i]+', '; //二组:1 -> 352400, 2 -> 348500,
- }
- group1+=n+' -> '+first[n];
- group2+=n+' -> '+second[n];
- WSH.Echo('数据一\n'+group1+'\n\n数据二\n'+group2);
复制代码
作者: for_flr 时间: 2022-5-23 10:51
- #&cls&powershell "iex(gc '%~0'|out-string)"&exit\b
- $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.}"
- $one=[regex]::matches($a,'(?<=\{)\d+(?=\.)').value
- $two=[regex]::matches($a,'(?<=\s)\d+(?=\.)').value
- (1..$one.count|%{"$_->"+$one[$_-1]}) -join ","
- (1..$two.count|%{"$_->"+$two[$_-1]}) -join ","
- 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 键继续...:
作者: aloha20200628 时间: 2022-5-23 13:38
给一个纯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
作者: for_flr 时间: 2022-5-23 15:54
- @echo off
- setlocal enabledelayedexpansion
- 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.}"
- set str=%str:{=%
- set str=%str:}=%
- set /a l=m=n=1
- for %%a in (%str%) do (
- set /a odd=l%%2
- set /a l+=1
- for /f "delims=." %%i in ("%%a") do (
- if !odd!==1 (
- set "line1=!line1! !m! -> %%i,"
- set /a m+=1
- ) else (
- set "line2=!line2! !n! -> %%i,"
- set /a n+=1
- )
- )
- )
- echo;!line1!
- echo;!line2!
- pause>nul
复制代码
作者: JK_m 时间: 2022-5-24 20:00
谢谢您
热心网友,明天测试下
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |