标题: 还是两个文本输出的问题:按列合并两个文本文件
[打印本页]
作者: bat-zw
时间: 2008-4-2 17:12
标题: 还是两个文本输出的问题:按列合并两个文本文件
a.txt
aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb
cccccccccccccccc dddddddddddddddddddd
eeeeeeeeeeeeeeee ffffffffffffffffffff
gggggggggggggggg hhhhhhhhhhhhhhhhhhhh
iiiiiiiiiiiiiiii jjjjjjjjjjjjjjjjjjjj
b.txt
1111111111111111111111 222222222222222222222222
3333333333333333333333 555555555555555555555555
9999999999999999999999 7777777777777777777777777
8888888888888888888888 4444444444444444444444444
6666666666666666666666 0000000000000000000000000
通过批处理输出内容如下:
aaaaaaaaaaaaaaaa 222222222222222222222222
bbbbbbbbbbbbbbbbbbbb 1111111111111111111111
cccccccccccccccc 555555555555555555555555
dddddddddddddddddddd 3333333333333333333333
eeeeeeeeeeeeeeee 7777777777777777777777777
ffffffffffffffffffff 9999999999999999999999
gggggggggggggggg 4444444444444444444444444
hhhhhhhhhhhhhhhhhhhh 8888888888888888888888
iiiiiiiiiiiiiiii 0000000000000000000000000
jjjjjjjjjjjjjjjjjjjj 6666666666666666666666
要求:
1 不生成任何临时文件
2 尽量简洁代码
3 a.txt和b.txt可互换
4 效率高
===================================
到目前最完美的方案(17楼):
@echo off
set n=-1
for /f "tokens=1,2" %%i in (a.txt) do (
call :lp %%i %%j
)
pause&goto :eof
:lp
set /a n+=1
set m=skip=%n%
if "%m%"=="skip=0" set m=
for /f "%m% tokens=1,2" %%i in (b.txt) do (
echo %1 %%j&echo %2 %%i&goto :eof
)
───────────────── 版务记录 ─────────────────
执行:HAT
操作:在帖子标题中增加搜索关键字
说明:原标题"还是两个文本输出的问题:"不利于论坛搜索
───────────────── 版务记录 ─────────────────
[
Last edited by HAT on 2008-10-4 at 13:48 ]
作者: bat-zw
时间: 2008-4-2 17:20
标题: 没人来试试吗?
我自己来抛砖引玉了,如下:
@echo off
set n=-1
:begin
set /a n+=1
set m=skip=%n%
if "%m%"=="skip=0" set m=
for /f "%m% tokens=1,2" %%i in ('findstr /n .* a.txt') do call :lp %%i %%j
goto :eof
:lp
for /f "%m% tokens=1,2" %%i in ('findstr /n .* b.txt') do (
set a=%1
set b=%%i
setlocal enabledelayedexpansion
if "!a:~,2!"=="!b:~,2!" echo !a:~2,50! %%j&echo %2 !b:~2,50!
endlocal
goto begin
)
这个代码最大的问题就是效率的问题。
作者: Climbing
时间: 2008-4-2 17:20
试试ruby吧,应该是很简单的事儿。
作者: bat-zw
时间: 2008-4-2 18:36
标题: 简洁2楼代码并提高效率如下:
@echo off
set n=0
for /f "tokens=1,2" %%i in ('findstr /n .* a.txt') do (
set /a n+=1
set a=%%i
setlocal enabledelayedexpansion
if "!a:~,1!"=="!n!" call :lp %%i %%j
endlocal
)
goto :eof
:lp
for /f "tokens=1,2" %%i in ('findstr /n .* b.txt') do (
set a=%1
set b=%%i
if "!b:~,1!"=="!n!" echo !a:~2,50! %%j&echo %2 !b:~2,50!&goto :eof
)
[
Last edited by zw19750516 on 2008-4-2 at 06:40 PM ]
作者: huahua0919
时间: 2008-4-2 18:39
!a:~2,50!
这样做是不是通用性就降低了
作者: bat-zw
时间: 2008-4-2 18:54
标题: 增加通用性:
Quote: |
Originally posted by huahua0919 at 2008-4-2 18:39:
!a:~2,50!
这样做是不是通用性就降低了 |
|
@echo off
set n=0
for /f "tokens=1,2" %%i in ('findstr /n .* a.txt') do (
set /a n+=1
set a=%%i
setlocal enabledelayedexpansion
if "!a:~,1!"=="!n!" call :lp %%i %%j
endlocal
)
pause&goto :eof
:lp
for /f "tokens=1,2" %%i in ('findstr /n .* b.txt') do (
set a=%1
set b=%%i
if "!b:~,1!"=="!n!" set a=!a:*:=!&set b=!b:*:=!&echo !a! %%j&echo %2 !b!&goto :eof
)
作者: zh159
时间: 2008-4-2 19:49
@echo off
set n=0
for /f "tokens=1,2 delims=" %%i in (a.txt) do (
call :lp %%i
set /a n+=1
)
pause
exit
:lp
if %n% gtr 0 for /f "skip=%n% tokens=1,2" %%i in (b.txt) do echo %1 %%j&echo %2 %%i&goto :eof
for /f "tokens=1,2" %%i in (b.txt) do echo %1 %%j&echo %2 %%i&goto :eof
作者: terse
时间: 2008-4-2 20:43
a和b文件行数不一样怎么办啊
@echo off
for /f "tokens=1,2" %%i in (a.txt) do call :loop %%i %%j
for /f "%n% tokens=1,2" %%i in (b.txt) do echo/%%j&echo/%%i
for /f "%n% tokens=1,2" %%i in (a.txt) do echo/%%i&echo/%%j
pause
goto :eof
:loop
for /f "%n% tokens=1-3 delims=: " %%i in ('findstr /n .* "b.txt"') do (
set n=skip=%%i&set str=%%j
if defined str (call set/p=%1 %%k<nul&echo.&call set/p=%2 %%str%%<nul&echo.) else call set/p=%1<nul&echo.&call set/p=%2<nul&echo.
goto :eof
)
楼主回复 :行数不一样,就不存在用这种输出方式了
这样也可以:
@echo off
for /f "tokens=1,2" %%i in (a.txt) do set/p=%%i <nul&call :loop %%i %%j
pause
goto :eof
:loop
for /f "%n% tokens=1-3 delims=: " %%i in ('findstr /n .* "b.txt"') do (
set n=skip=%%i&set str=%%j&set/p=%%k<nul&echo.
if defined str (call set/p=%2 %%str%%<nul&echo.) else call set/p=%2 %1<nul&echo.
goto :eof
)
[
Last edited by terse on 2008-4-2 at 09:04 PM ]
作者: bat-zw
时间: 2008-4-2 20:43
Quote: |
Originally posted by zh159 at 2008-4-2 19:49:
[code]@echo off
set n=0
for /f "tokens=1,2 delims=" %%i in (a.txt) do (
call :lp %%i
set /a n+=1
)
pause
exit
:lp
if %n% gtr 0 for /f "skip=%n% tokens=1,2" %% ... |
|
老兄的代码怕是存在效率问题啊。
作者: bat-zw
时间: 2008-4-2 20:47
Quote: |
Originally posted by terse at 2008-4-2 20:43:
a和b文件行数不一样怎么办啊
@echo off
for /f "tokens=1,2" %%i in (a.txt) do set/p=%%i <nul&call :loop %%j
for /f "%n% tokens=1,2" %%i in (b.txt) do echo/%%j ... |
|
行数不一样,就不存在用这种输出方式了。
作者: bat-zw
时间: 2008-4-2 23:53
标题: 继续提效:
@echo off
set n=0
for /f "tokens=1,2" %%i in ('findstr /n .* a.txt') do (
set /a n+=1
set /a m=n-1
set a=%%i
setlocal enabledelayedexpansion
if "!a:~,1!"=="!n!" call :lp %%i %%j
endlocal
)
pause&goto :eof
:lp
set skip=skip=!m!
if "!skip!"=="skip=0" set skip=
for /f "%skip% tokens=1,2" %%i in ('findstr /n .* b.txt') do (
set a=%1
set b=%%i
if "!b:~,1!"=="!n!" set a=!a:*:=!&set b=!b:*:=!&echo !a! %%j&echo %2 !b!
&goto :eof
)
作者: zh159
时间: 2008-4-3 00:10
Quote: |
Originally posted by zw19750516 at 2008-4-2 20:43:
老兄的代码怕是存在效率问题啊。 |
|
不知道你试过了没有......
你6楼代码测试:
Quote: |
开始时间:22:47:10.00
结束时间:22:47:12.95
需时:2.95秒 |
|
你11楼代码测试:
Quote: |
开始时间:22:52:40.00
结束时间:22:52:43.48
需时:3.48秒 |
|
我7 楼的代码测试
Quote: |
开始时间:22:47:20.00
结束时间:22:47:20.03
需时:0.03秒 |
|
测试方式:
Quote: |
@echo off
:loop
set time_=%time%
if not %time_:~-4% == 0.00 goto loop
set n=0
for /f "tokens=1,2 delims=" %%i in (a.txt) do (
call :lp %%i
set /a n+=1
)
echo %time_%
echo %time%
pause
exit
:lp
if %n% gtr 0 for /f "skip=%n% tokens=1,2" %%i in (b.txt) do echo %1 %%j&echo %2 %%i&goto :eof
for /f "tokens=1,2" %%i in (b.txt) do echo %1 %%j&echo %2 %%i&goto :eof |
|
作者: bat-zw
时间: 2008-4-3 00:31
Quote: |
Originally posted by zh159 at 2008-4-3 00:10:
不知道你试过了没有......
你6楼代码测试:
你11楼代码测试:
我7 楼的代码测试
测试方式:
|
|
是findstr的问题吗?
作者: terse
时间: 2008-4-3 02:18
就效率方面 应该是zh159兄的更胜一筹
作者: jvive
时间: 2008-4-3 10:22
本人测试过100000次if 否定判断用时1.63秒, 执行效率
61349次/秒
call 空语句 10000次 用时10.907秒,执行效率
917.496次/秒
findstr .* test.txt>nul 100次 用时3.55秒, 执行效率
28.169次/秒
(test为 一个10行文字的文本文件)
@echo off||{jv|08-04-03}
echo 当前时间:%time%
setlocal enabledelayedexpansion
for /f "tokens=1-2" %%a in (a.txt) do (
set/a n+=1,m=0
for /f "tokens=1-2" %%c in (b.txt) do (
if !m! leq !n! set/a m+=1
if !m!==!n! echo %%a %%d&echo %%b %%c
))
echo 当前时间:%time%
pause>nul
附图:
[
Last edited by jvive on 2008-4-13 at 01:02 PM ]
作者: bat-zw
时间: 2008-4-3 10:32
标题: 真的太感谢大家了:
感谢大家的关注和测试,现在我明白了,findstr绝不可滥用了,再次感谢大家!!!
作者: bat-zw
时间: 2008-4-3 11:47
标题: 再次精减提效:
这应该是比较完美的方案了:
@echo off
set n=-1
for /f "tokens=1,2" %%i in (a.txt) do (
call :lp %%i %%j
)
pause&goto :eof
:lp
set /a n+=1
set m=skip=%n%
if "%m%"=="skip=0" set m=
for /f "%m% tokens=1,2" %%i in (b.txt) do (
echo %1 %%i&echo %%j %2&goto :eof
)
[
Last edited by zw19750516 on 2008-4-3 at 12:18 PM ]