For the sake of example I have two files:
FileA.txt
1.
2.
3.
FileB.txt
ABC
DEF
GHI
And I want to merge in order to obtain:
Output.txt
1.ABC
2.DEF
3.GHI
I need to merge each line, placing the contents of one file at the beginning of the line and the contents of the other file after it. I can't do it manually, as there are more than 30 000 lines. I'm on Windows, but I can install Linux if necessary.
I did search on this site and on the internet in general, but only found either really old and outdates posts, solutions that merge several files, but not line by line, or ones that require too much coding knowledge, which I lack. I also tried to use excel to merge two columns, but I can't get around the 256 character limit. If there is already an answer to this, sorry, I have missed it. Any help is appreciated. Thank you.
56 Answers
Use paste command, e.g.
$ paste FileA.txt FileB.txt
1. ABC
2. DEF
3. GHINote: Add -d' ' to avoid adding space between the columns.
To redirect output to the new file, append: > NewFile.txt.
On Windows, you can install Git Shell or Cygwin. Or use Docker for Windows.
3On linux, a simple command taking advantage of diff (which is installed on pretty much every unix/linux system by default) and its -y flag (side by side comparison) and sed which removes unwanted spaces/tabs inserted by the diff process.
$ diff -y 1.txt 2.txt | sed 's/\s*|\t*//g'
1.a
2.b
3.cGiven the files 1.txt:
1.
2.
3.and 2.txt:
a
b
cThe above assumes you have files of equal number of lines and that each line is different, which seems to be the case from your question.
2Use Vim editor, e.g.
- Open two files side-by-side:
vim FileA.txt FileB.txt -O. In the first file, vertically select 2 columns by hitting these keystrokes:
- 1, Shift-G (go to the beginning of the file).
- Control-V (enter visual block mode).
- Shift-G, $ (select two columns).
- y (yank/copy into buffer).
- Go to the next file by hitting: Control-w, w.
- Make sure you're in the first line by: 1, Shift-G.
- In the first line, hit: Shift-P to paste vertically.
- Save and quit (:wq).
See the demo:
To automate above steps for larger files, either record a macro and invoke it again, or you can use ex command (part of Vim) to edit the files non-interactively, for examples, see: How to edit files non-interactively (e.g. in pipeline)?
Go can achieve similar in Sublime Text, either by using Vintage (Vim) plugin, or by selecting the column with Alt vertically, copy and paste in another file.
A general Linux solution is:-
E1=""; E2=""
{ while true do read -r <&3 && l1="$REPLY" || l1="" E1=e read -r <&4 && l2="$REPLY" || l2="" E2=e [ "$E1$E2" == ee ] && break echo "$l1$l2" done
} 3<"$1" 4<"$2"I have formatted this as a script for legibility, but it can be entered as a long command line by replacing the new-lines with semi-colons, and replacing $1 and $2 by the paths to the files to be merged.
This works as follows:-
E1andE2are end-of-file flags;- Two input streams (3 and 4) are opened from the two file paths passed;
- A line is read from each file and set in variables
l1andl2respectively; - Note that
read -r l1strips leading and trailing blanks, hence the more complex code to setl1(andl2); - The loop terminates when both files reach EOF, although it is a trivial modification to terminate on either file reaching EOF;
- The
echowill go to standard out, or>"$3"could be added to the line, making the output file the third parameter; - The
echocommand can be extended if you want to add a delimiter string to separate the text from each file.
The above script should work in WSL (Windows Subsystem for Linux) in Windows 10, or CygWin in earlier Windows releases.
It would be possible to implement in cmd, but I wouldn't want to try, though it would be straightforward in the enhanced cmd replacement freeware TCC/LE. It should also be possible with PowerShell, but I don't have a lot of expertise in that, as I use mostly Linux.
Use CudaText editor with multi-selections feature.
- Select all in file-1
- Call "Selection / Split selection into lines" in file-1
- Select all in file-2
- Call "Selection / Split selection into lines" in file-2
- Copy to clipboard (many lines) in file-2
- In file-1, press End to put carets to line ends
- !! Make sure count of carets in file-1 equals to count of lines copied to clipboard (if unneeded caret at end - Ctrl+click it to delete it)
- If they equal, at line-ends, press Ctrl+V (Paste) - this pastes clipboard line by line
Following steps can be taken to achieve this:
- Copy contents of file
FileA.txtin column A of Excel sheet - Copy contents of file
FileB.txtin column B of Excel sheet - Save Excel file as
.txtfile - Open
.txtfile inNotepad++ - Replace
TABcharacter with `.'