1.

Solve : Embedded for loop.?

Answer»

Quote from: BETTY on August 27, 2011, 03:36:08 AM

@Ghostdog74 - Thank you for your interest.

Paste appears not to do what I want in that it merges corresponding lines from each file, it does not appear to interleave the lines.

yes, it does this

(File 1)
Line A
Line B

(File 2)
Line 1
Line 2

Output

Line A Line 1
Line B Line 2

You can select other delimiters than the dafault TAB but essentially that's what it does I think. If the delimiter could be CR/LF that might be a solution?


Yes, salmon is right, if Betty wants to intersperse the lines,
Code: [Select]paste -d "\n" fileA.txt fileB.txt
Otherwise, the default places the lines "side by side"Quote
METHOD 1 (straight code, no subroutine, process all lines of File2 for each line of File1) 83 seconds
Method 2 (subroutine, jump out when correct line number match reached) 51 seconds (so not QUITE 50%)
Method 3 (subroutine, use SKIP and jump out after reading 1 line) 19 seconds (a bit more than 50%)

Hi Salmon Trout,

Could you please help test the performace of this one:

Code: [Select]@echo off
setlocal enabledelayedexpansion
(for /f "delims=" %%a in (file1.txt) do (
set /p _f=
echo,%%a
echo,!_f!
))<file2.txt >file3.txtQuote
Both files CONTAIN the same number of lines.

I understand the OP SPECS, but what if the files are of unequal length? Batch code does not support arrays, however borrowing from REXX, you can mimic the stem.tails technique. I'm also a big fan of prompts for non-automation scripts. Makes them more generic.

This is an alternate approach, nothing more:

Code: [Select]@echo off
setlocal enabledelayedexpansion

:file1
set /p file1=Enter File 1 Label:
if not exist %file1% goto file1

:file2
set /p file2=Enter File 2 Label:
if not exist %file2% goto file2

for /f "tokens=* delims=" %%y in (%file1%) do (
call set /a idx=%%idx%%+1
call set array.%%idx%%=%%y
)

set array.0=%idx%

for /f "tokens=* delims=" %%i in (%file2%) do (
call set /a index+=1
if !index! LEQ %array.0% (
call echo %%array.!index!%% >> c:\temp\merged.txt
)
call echo %%i >> c:\temp\merged.txt
)

if %index% LSS %array.0% (
for /l %%i in (%index%, 1, %array.0%) do (
echo !array.%%i! >> c:\temp\merged.txt
)
)

Powershell and VBScript can also be used with varying degrees of simplicity.

Quote from: CN-DOS on August 31, 2011, 05:20:18 AM
Could you please help test the performace of this one:

What performance result did you get?

I used timeit.exe to count the time, and 1000 lines for each file.

Method 3 of Salmon Trout:
Elapsed Time: 0:00:06.364
Process Time: 0:00:04.368

Method of CN-DOS:
Elapsed Time: 0:00:00.468
Process Time: 0:00:00.265

BTW, the PMs from NatHeim are realy boring. Is it possible for moderator to disable him to use PM? Or does this forum have black list function, so I can put him in it?Method of CN-DOS 0.38 seconds elapsed time.

In your profile you have an Ignore list




Discussion

No Comment Found