标题: [文本处理] 【已解决】批处理如何按列进行数据分解? [打印本页]
作者: 思想之翼 时间: 2013-5-18 22:56 标题: 【已解决】批处理如何按列进行数据分解?
本帖最后由 思想之翼 于 2013-5-19 23:03 编辑
txt文本的数据格式为:时间序号+63列数据(数据为0-9之间的数字),比如
0000001 2 5 6 9 1 4....
0000002 7 3 8 0 1 3....
...
现在欲将63列数据按列分解,分别写入63个新建的txt文本,每个文本数据格式为:
0000001 2
0000002 7
......
恳望得到大家的帮助!
作者: xxpinqz 时间: 2013-5-19 00:49
- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%a in (a.txt) do (
- set /a m=0,n+=1
- for %%b in (%%a) do set /a m+=1&set #!n!!m!=%%b
- )
- for /l %%a in (2,1,%m%) do (
- (for /l %%b in (1,1,%n%) do (
- echo !#%%b1! !#%%b%%a!
- ))>a_1-%%a.txt
- )
复制代码
是不是这样的
作者: 思想之翼 时间: 2013-5-19 14:20
回复 2# xxpinqz
谢谢帮助!试验了下,代码长时间运行,就是不出结果,好似假死一般。
作者: terse 时间: 2013-5-19 14:34
貌似发过 类似问题
即便不全是雷同 想你也不难修改的
作者: tmplinshi 时间: 2013-5-19 14:57
- gawk "{ for (i=2; i<=NF; i++){print $1 \"\t\" $i >f i-1 \".txt\"} }" f="a_" a.txt
复制代码
作者: xxpinqz 时间: 2013-5-19 19:05
本帖最后由 xxpinqz 于 2013-5-19 19:10 编辑
回复 3# 思想之翼
原来没测试,刚测了一下,文本行数多了还真会假死掉,主要是set变量太耗时了,占用2/3不止,300行以上就得55秒左右,效率太低了。
改成这样,效率也不高,2000行64列的文本大约要3秒15左右.还是第三方的快。- @echo off
- md tmp 2>nul
- for /l %%a in (2,1,64) do call:sc "tokens=1,%%a delims= " "%%a"
- exit /b
- :sc
- (for /f %1 %%b in (a.txt) do echo %%b %%c)>tmp\a_1-%~2.txt
- goto :eof
复制代码
备注:晕,这个不能用,你还是用第三方的吧,我自个儿晚点再练练手改改,没注意tokens超32会出错
作者: terse 时间: 2013-5-19 19:54
- @echo off&setlocal enabledelayedexpansion
- for /f "tokens=1*" %%i in (a.txt) do (
- set n=
- for %%j in (%%j) do (
- set /a n+=1
- >>"a_!n!.txt" echo %%i %%j
- )
- )
复制代码
作者: xxpinqz 时间: 2013-5-19 21:36
回复 7# terse
这写法很高明哦,就是行数多了打开文件未免太频繁了
作者: xxpinqz 时间: 2013-5-19 21:58
- @echo off&setlocal enabledelayedexpansion
- md tmp 2>nul
- set "n=6"
- for /l %%a in (2,1,64) do (
- set/a n+=2
- (for /f "delims=" %%b in (a.txt) do (
- set "str=%%b"
- for %%c in (!n!) do echo !str:~,8!!str:~%%c,2!
- ))>tmp\a_1-%%a.txt
- )
复制代码
题解题.
作者: 思想之翼 时间: 2013-5-19 23:05
谢谢各位热心帮助!圆满解决问题。
作者: 思想之翼 时间: 2013-5-20 00:12
本帖最后由 思想之翼 于 2013-5-20 18:40 编辑
回复 5# tmplinshi
谢谢您的帮助!菜鸟的我还是感觉下面的VBS方便些,虽然是最笨的。
有个问题需要求教:这个VBS代码按列分解后,每个文本的数据最后(最底)留有一空行,导入excel运行宏时会报错,如何修改代码,就可以去除这一空行?
ar01 = Array(01)
ar02 = array(02)
ar03 = array(03)
ar04 = Array(04)
ar05 = array(05)
ar06 = array(06)
ar07 = Array(07)
ar08 = array(08)
ar09 = array(09)
ar10 = Array(10)
ar11 = array(11)
ar12 = array(12)
ar13 = Array(13)
ar14 = array(14)
ar15 = array(15)
ar16 = Array(16)
ar17 = array(17)
ar18 = array(18)
ar19 = Array(19)
ar20 = array(20)
ar21 = array(21)
ar22 = Array(22)
ar23 = array(23)
ar24 = array(24)
ar25 = Array(25)
ar26 = array(26)
ar27 = array(27)
ar28 = Array(28)
ar29 = array(29)
ar30 = array(30)
ar31 = Array(31)
ar32 = array(32)
ar33 = array(33)
ar34 = Array(34)
ar35 = array(35)
ar36 = array(36)
ar37 = Array(37)
ar38 = array(38)
ar39 = array(39)
ar40 = Array(40)
ar41 = array(41)
ar42 = array(42)
ar43 = Array(43)
ar44 = array(44)
ar45 = array(45)
ar46 = Array(46)
ar47 = array(47)
ar48 = array(48)
ar49 = Array(49)
ar50 = array(50)
ar51 = array(51)
ar52 = Array(52)
ar53 = array(53)
ar54 = array(54)
ar55 = Array(55)
ar56 = array(56)
ar57 = array(57)
ar58 = Array(58)
ar59 = array(59)
ar60 = array(60)
ar61 = Array(61)
ar62 = array(62)
ar63 = array(63)
Set FSO = CreateObject("Scripting.FileSystemObject")
getColumn ar01,"01.txt"
getColumn ar02,"02.txt"
getColumn ar03,"03.txt"
getColumn ar04,"04.txt"
getColumn ar05,"05.txt"
getColumn ar06,"06.txt"
getColumn ar07,"07.txt"
getColumn ar08,"08.txt"
getColumn ar09,"09.txt"
getColumn ar10,"10.txt"
getColumn ar11,"11.txt"
getColumn ar12,"12.txt"
getColumn ar13,"13.txt"
getColumn ar14,"14.txt"
getColumn ar15,"15.txt"
getColumn ar16,"16.txt"
getColumn ar17,"17.txt"
getColumn ar18,"18.txt"
getColumn ar19,"19.txt"
getColumn ar20,"20.txt"
getColumn ar21,"21.txt"
getColumn ar22,"22.txt"
getColumn ar23,"23.txt"
getColumn ar24,"24.txt"
getColumn ar25,"25.txt"
getColumn ar26,"26.txt"
getColumn ar27,"27.txt"
getColumn ar28,"28.txt"
getColumn ar29,"29.txt"
getColumn ar30,"30.txt"
getColumn ar31,"31.txt"
getColumn ar32,"32.txt"
getColumn ar33,"33.txt"
getColumn ar34,"34.txt"
getColumn ar35,"35.txt"
getColumn ar36,"36.txt"
getColumn ar37,"37.txt"
getColumn ar38,"38.txt"
getColumn ar39,"39.txt"
getColumn ar40,"40.txt"
getColumn ar41,"41.txt"
getColumn ar42,"42.txt"
getColumn ar43,"43.txt"
getColumn ar44,"44.txt"
getColumn ar45,"45.txt"
getColumn ar46,"46.txt"
getColumn ar47,"47.txt"
getColumn ar48,"48.txt"
getColumn ar49,"49.txt"
getColumn ar50,"50.txt"
getColumn ar51,"51.txt"
getColumn ar52,"52.txt"
getColumn ar53,"53.txt"
getColumn ar54,"54.txt"
getColumn ar55,"55.txt"
getColumn ar56,"56.txt"
getColumn ar57,"57.txt"
getColumn ar58,"58.txt"
getColumn ar59,"59.txt"
getColumn ar60,"60.txt"
getColumn ar61,"61.txt"
getColumn ar62,"62.txt"
getColumn ar63,"63.txt"
Sub getColumn(ar,filename)
Set file = FSO.OpenTextFile("a.txt",1)
Do While file.AtEndOfStream <> True
n = Split(file.ReadLine)
str = str & n(0)
For Each a In ar
str = str & " " & n(a)
Next
str = str & vbCrLf
Loop
file.Close
Set file = FSO.CreateTextFile(filename,True)
file.Write str
file.Close
End Sub
作者: Batcher 时间: 2013-5-20 12:52
回复 11# 思想之翼
文本总共多少行?文件大小有多少MB?
gawk和VBS的执行时间分别是多少?
作者: 思想之翼 时间: 2013-5-20 18:42
回复 12# Batcher
非常感谢您给予的大力帮助!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |