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

[文本处理] 【已解决】批处理如何按列进行数据分解?

本帖最后由 思想之翼 于 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
......
恳望得到大家的帮助!
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%a in (a.txt) do (
  3.   set /a m=0,n+=1
  4.   for %%b in (%%a) do set /a m+=1&set #!n!!m!=%%b
  5. )
  6. for /l %%a in (2,1,%m%) do (
  7.   (for /l %%b in (1,1,%n%) do (
  8.      echo !#%%b1! !#%%b%%a!
  9.   ))>a_1-%%a.txt
  10. )
复制代码
是不是这样的
初学BAT,非专业。代码不适当之处还望前辈们多多指点。在此表示感谢!

TOP

回复 2# xxpinqz

谢谢帮助!试验了下,代码长时间运行,就是不出结果,好似假死一般。

TOP

貌似发过 类似问题
即便不全是雷同  想你也不难修改的

TOP

  1. gawk "{ for (i=2; i<=NF; i++){print $1 \"\t\" $i >f i-1 \".txt\"} }" f="a_" a.txt
复制代码
1

评分人数

TOP

本帖最后由 xxpinqz 于 2013-5-19 19:10 编辑

回复 3# 思想之翼
原来没测试,刚测了一下,文本行数多了还真会假死掉,主要是set变量太耗时了,占用2/3不止,300行以上就得55秒左右,效率太低了。
改成这样,效率也不高,2000行64列的文本大约要3秒15左右.还是第三方的快。
  1. @echo off
  2. md tmp 2>nul
  3. for /l %%a in (2,1,64) do call:sc "tokens=1,%%a delims= " "%%a"
  4. exit /b
  5. :sc
  6. (for /f %1 %%b in (a.txt) do echo %%b %%c)>tmp\a_1-%~2.txt
  7. goto :eof
复制代码
备注:晕,这个不能用,你还是用第三方的吧,我自个儿晚点再练练手改改,没注意tokens超32会出错
1

评分人数

初学BAT,非专业。代码不适当之处还望前辈们多多指点。在此表示感谢!

TOP

  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "tokens=1*" %%i in (a.txt) do (
  3. set n=
  4. for %%j in (%%j) do (
  5. set /a n+=1
  6. >>"a_!n!.txt" echo %%i %%j
  7.         )
  8. )
复制代码
1

评分人数

TOP

回复 7# terse
这写法很高明哦,就是行数多了打开文件未免太频繁了
初学BAT,非专业。代码不适当之处还望前辈们多多指点。在此表示感谢!

TOP

  1. @echo off&setlocal enabledelayedexpansion
  2. md tmp 2>nul
  3. set "n=6"
  4. for /l %%a in (2,1,64) do (
  5.   set/a n+=2
  6.   (for /f "delims=" %%b in (a.txt) do (
  7.     set "str=%%b"
  8.     for %%c in (!n!) do echo !str:~,8!!str:~%%c,2!
  9.   ))>tmp\a_1-%%a.txt
  10. )
复制代码
题解题.
1

评分人数

初学BAT,非专业。代码不适当之处还望前辈们多多指点。在此表示感谢!

TOP

谢谢各位热心帮助!圆满解决问题。

TOP

本帖最后由 思想之翼 于 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

TOP

回复 11# 思想之翼


文本总共多少行?文件大小有多少MB?
gawk和VBS的执行时间分别是多少?
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 12# Batcher


非常感谢您给予的大力帮助!

TOP

返回列表