Board logo

标题: [文本处理] 【已解决】批处理如何将多个txt文本行行对应合并成一行 [打印本页]

作者: elec    时间: 2014-7-4 09:36     标题: 【已解决】批处理如何将多个txt文本行行对应合并成一行

本帖最后由 pcl_test 于 2016-7-22 23:11 编辑

我有5个txt文件,r1.txt ~ r5.txt 每个文件均是500行的短句

r1.txt
  1. No matter if you're planning a conference for the members of your fraternity, or you're planning an event to raise funds for cancer research
  2. one of the best ways to manage attendance for your event is through online registration. In the past, event organizers had to rely on fax machines and phones to confirm attendance.
  3. Thanks to recent advances, you can now do everything from registration and confirmation to payments and follow up information gathering online..
  4. Their bodies stored the food in the form of fat. When food was scarce, they had little, at times nothing to eat, the body could depend on the stores of fat for the necessary energy for survival.
  5. Hence when food is available, the body stores what it can for the bad time..
  6. Most Americans have a high school diploma, but, there are still approximately just under 40 million who do not.
  7. Not having one sometimes causes people to lie to prospective employers, or acquire a fake one or even purchase one off the computer.
  8. This is why it is necessary for companies to verify a high school diploma.Icons in Daily LifeThere are some beautiful churches throughout the world.
  9. This Dogs of the Index strategy, popularized by Michael B. O'Higgins in the book "Beating The Dow" (HarperCollins, 1991), reveals how low yielding stocks whose prices increase and whose dividend yields
  10. Not all air taxi operators use jets. SATSair, which serves the southeastern United States from Philadelphia to the Florida Keys, flies single engine propeller equipped Cirrus planes.
  11. ......
复制代码
我想合并r1.txt ~ r5.txt 的每一行,输出到result.txt 可以实现吗??
作者: xxpinqz    时间: 2014-7-4 14:03

  1. @echo off&setlocal enabledelayedexpansion
  2. (for /f "tokens=1-2* delims=:" %%a in ('findstr /n . r?.txt') do (
  3.     set "n=00000%%b"
  4.     echo,!n:~-5!%%~na:%%c
  5. ))>tmp
  6. (for /f "tokens=1* delims=:" %%a in ('sort tmp') do (
  7.     set/p"=%%b"
  8.     set/am+=1,k=m%%5
  9.     if !k! equ 0 echo,
  10. ))<nul>result.txt
  11. del tmp
复制代码

作者: digger    时间: 2014-7-4 16:09

本帖最后由 digger 于 2014-7-4 16:13 编辑
  1. @echo off&setlocal enabledelayedexpansion
  2. set/a a=0,b=0,c=0,d=0,e=0
  3. :a5
  4. for /f "delims=" %%i in ('more +!a! 1.txt') do (
  5.    set/a a+=1&set b1=%%i
  6.   for /f "delims=" %%j in ('more +!b! 2.txt') do set/a b+=1&set b2=%%j&goto :a1
  7. :a1
  8.   for /f "delims=" %%k in ('more +!c! 3.txt') do set/a c+=1&set b3=%%k&goto :a2
  9. :a2
  10.   for /f "delims=" %%l in ('more +!d! 4.txt') do set/a d+=1&set b4=%%l&goto :a3
  11. :a3
  12.   for /f "delims=" %%m in ('more +!e! 5.txt') do set/a e+=1&set b5=%%m&goto :a4
  13. )
  14. :a4
  15. for /f "delims=" %%n in ("!b1!!b2!!b3!!b4!!b5!") do if not defined %%n (set %%n=aa&echo.%%n) else echo.完成&pause&exit
  16. set h=0&set b1=&set b2=&set b3=&set b4=&set b5=
  17. goto a5
复制代码
速度慢,空行不考慮在內,循環取決於第一個FOR的1.txt,如果有完全相同的會有問題
作者: terse    时间: 2014-7-4 17:45

如果固定行数的话
这样效率提升点
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in ('dir /b/a-d r?.txt') do if not defined file set "file=%%i"
  3. copy !file! _tem.txt >nul
  4. for /f "skip=1 delims=" %%i in ('dir /b/a-d r?.txt') do (
  5.     (for /f "delims=" %%a in (_tem.txt) do (
  6.         set /ps=
  7.         echo,%%a!s!
  8.     ))<%%i >$
  9.     move $ _tem.txt >nul
  10. )
  11. pause
复制代码

作者: DAIC    时间: 2014-7-5 12:46

具体如何每一对应行?给个例子吧
作者: elec    时间: 2014-7-6 19:31

本帖最后由 elec 于 2014-7-6 19:33 编辑

回复 5# DAIC


    就是1~5个文件。每个文件的行数都一样。
  1. 1.txt   2.txt    3.txt    4.txt   5.txt
  2. A        A        A        A        A
  3. B        B        B        B        B
  4. C        C        C        C        C
  5. D        D        D        D        D
  6. ......
复制代码
合并完后
  1. AAAAA
  2. BBBBB
  3. CCCCC
  4. DDDDD
  5. ......
复制代码

作者: CrLf    时间: 2014-7-6 19:58

保存为 合并.js:
  1. var arr=['a.txt','b.txt','c.txt','d.txt','e.txt']
  2. var ts=[]
  3. var text =''
  4. var fso=new ActiveXObject('Scripting.FileSystemObject')
  5. for(i in arr)ts.push(fso.OpenTextFile(arr[i],1))
  6. while(!ts[0].AtEndOfStream){
  7. for(i in ts){
  8. if(!ts[i].AtEndOfStream)text+=ts[i].ReadLine()
  9. }
  10. text+='\r\n'
  11. }
  12. fso.CreateTextFile('输出.txt',true).Write(text)
复制代码

作者: CrLf    时间: 2014-7-6 20:14

本帖最后由 CrLf 于 2014-7-6 20:20 编辑

如果是我自己用,更倾向 sed 和 gnusort...
  1. @findstr /n .* a.txt b.txt c.txt d.txt e.txt|sort -n -t":"|sed "s/^[0-9]*://" >输出.txt
复制代码
当然,gawk 也是极好的,但是写起来麻烦多了:
  1. gawk "{ar[FNR]=ar[FNR] $0}END{for(i in ar)print(ar[i])}" a.txt b.txt c.txt d.txt e.txt>输出.txt
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2