Solved Windows Batch FIle to combine two text files into one

August 9, 2017 at 05:13:43
Specs: Windows 7
Hello, how can i combine two text files into a third file :-

File A.txt
00:0 30-06-17 -1
00:1 30-06-17 10
00:2 30-06-17 11
00:3 30-06-17 6

File B.txt
00:0 30-06-17 10
00:1 30-06-17 2
00:2 30-06-17 43
00:3 30-06-17 220

Output File required C.txt
00:0 30-06-17 -1 10
00:1 30-06-17 10 2
00:2 30-06-17 11 43
00:3 30-06-17 6 220


See More: Windows Batch FIle to combine two text files into one

Reply ↓  Report •

✔ Best Answer
August 11, 2017 at 19:48:23
Alright, switching tactics to "keyed" vs positional approach - worked with your sample-data:
:: begin batch
@echo off & setlocal
> file_c (for /f "tokens=1-3" %%a in (file_a) do (
for /f "tokens=1-3" %%x in ('find "%%a %%b"^<file_b^|^|echo %%a %%b') do echo %%a %%b %%c %%z
)
)

message edited by nbrane



#1
August 9, 2017 at 18:23:34
Assuming these are purely positionally joined, and not by a key-value:

:: begin batch
@echo off & setlocal
set c=0
for /f "tokens=1-3" %%a in (file_a) do call :xx %%a %%b %%c
goto :eof

:xx
for /f "tokens=3" %%d in ('more +%c% file_b') do (
>>file_c echo %1 %2 %3 %%d
set /a c+=1
goto :eof
)
::===== end script
powershell probably has a one-liner as usual, but this worked ok in my simple test.


Reply ↓  Report •

#2
August 9, 2017 at 18:50:17
nbrane: powershell probably has a one-liner
Smallest FizzBuzz PowerShell solution I've seen was 62 characters long. There's no relation about that to anything in this thread, I just thought I'd mention it.

$aFile = gc "File A.txt"
$bFile = gc "File B.txt" | % { (-split $_)[-1] }
for ($i = 0; $i -lt $afile.Count; ++$i) { 
  $afile[$i],$bFile[$i] -join ' ' >> "New File.txt" 
}

From wiki.c2.com/?FizzBuzzTest:
1..100|%{(-join(@('Fizz')[$_%3],@('Buzz')[$_%5]),$_|?{$_})[0]}

How To Ask Questions The Smart Way


Reply ↓  Report •

#3
August 11, 2017 at 10:17:46
Thanks nbrane, it worked. But what can be done if
File A.txt is
00:0 30-06-17 10
00:1 30-06-17 2
00:2 30-06-17 43
00:3 30-06-17 22
00:4 30-06-17 33
00:5 30-06-17 45

File B.txt is
00:0 30-06-17 -1
00:1 30-06-17 10
00:2 30-06-17 11
00:3 30-06-17 6

Required output file is
00:0 30-06-17 10 -1
00:1 30-06-17 2 10
00:2 30-06-17 43 11
00:3 30-06-17 22 6
00:4 30-06-17 33
00:5 30-06-17 45


Reply ↓  Report •

Related Solutions

#4
August 11, 2017 at 19:48:23
✔ Best Answer
Alright, switching tactics to "keyed" vs positional approach - worked with your sample-data:
:: begin batch
@echo off & setlocal
> file_c (for /f "tokens=1-3" %%a in (file_a) do (
for /f "tokens=1-3" %%x in ('find "%%a %%b"^<file_b^|^|echo %%a %%b') do echo %%a %%b %%c %%z
)
)

message edited by nbrane


Reply ↓  Report •

#5
August 11, 2017 at 23:11:05
Thanks a lot..it worked !

Reply ↓  Report •

#6
August 13, 2017 at 07:59:00
I guess since I hopped on the PS train, I might as well stay on it 'til the end, and show a PS solution:
'a.txt', 'b.txt' | 
 foreach { gc $_ } |
 foreach -Begin { $records = @{} } { 
   $line = $_ -split ' ', 3
   $records[$line[0, 1] -join ' '] += , $line[2]
 } -End { $records.GetEnumerator() } |
 sort Name |
 foreach { "$($_.Name) $($_.Value -join ' ')" } |
 Out-File 'out.txt'

How To Ask Questions The Smart Way


Reply ↓  Report •

Ask Question