I'm stuck with this : I need to merge two text files in a single tab delimited text file, on a batch script. ex :
file1:
qwer
tyui
asdf
file2:
1345
6876
8796
file3:
qwer 1345
tyui 6876
asdf 8796
All I need in fact, is a equivalent to Unix command : paste -d "\t" file1 file2 > file3
Two quick options for combining text files. Open the two files you want to merge. Select all text (Command+A/Ctrl+A) from one document, then paste it into the new document (Command+V/Ctrl+V). Repeat steps for the second document.
In the Word Ribbon, click the Insert tab, click the down arrow next to Object, and select the Text from File option, as shown below. Select the file you want to merge into the current document and click Insert. Once completed, the text and other information from the document will be merged into the current document.
@echo off
set f1=1.txt
set f2=2.txt
set "sep= " % tab %
(
for /f "delims=" %%a in (%f1%) do (
setlocal enabledelayedexpansion
set /p line=
echo(%%a!sep!!line!
endlocal
)
)<%f2%
pause
goto :eof
The answer of walid2me is awesome, this is only a small mofication to show how to make it safe against characters like !^
in the file 1.txt
, the content of file 2.txt
is safe, a it is read with teh set/p
syntax.
@echo off
set f1=1.txt
set f2=2.txt
set "sep= " % tab %
(
setlocal DisableDelayedExpansion
for /f "delims=" %%a in (%f1%) do (
set "f1_line=%%a"
setlocal EnableDelayedExpansion
set /p f2_line=
echo(!f1_line!!sep!!f2_line!
endlocal
)
endlocal
)<%f2%
pause
goto :eof
As you can see, I only move the expansion of %%a
just before the setlocal EnableDelayedExpansion
There is still another small flaw, as set/p
strips trailing control characters, like single CR, LF and also TAB.
This affects only the file 2.txt.
In depth analysis about set /p
are at New technic: set /p can read multiple lines from a file
There's no native Windows command I know of that will do that, but there's a set of Unix tools for Windows here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With