Merge files line by line

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.

5

6 Answers

Use paste command, e.g.

$ paste FileA.txt FileB.txt
1. ABC
2. DEF
3. GHI

Note: 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.

3

On 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.c

Given the files 1.txt:

1.
2.
3.

and 2.txt:

a
b
c

The 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.

2

Use Vim editor, e.g.

  1. Open two files side-by-side: vim FileA.txt FileB.txt -O.
  2. In the first file, vertically select 2 columns by hitting these keystrokes:

    1. 1, Shift-G (go to the beginning of the file).
    2. Control-V (enter visual block mode).
    3. Shift-G, $ (select two columns).
    4. y (yank/copy into buffer).
  3. Go to the next file by hitting: Control-w, w.
  4. Make sure you're in the first line by: 1, Shift-G.
  5. In the first line, hit: Shift-P to paste vertically.
  6. Save and quit (:wq).

See the demo:

asciicast


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:-

  • E1 and E2 are 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 l1 and l2 respectively;
  • Note that read -r l1 strips leading and trailing blanks, hence the more complex code to set l1 (and l2);
  • The loop terminates when both files reach EOF, although it is a trivial modification to terminate on either file reaching EOF;
  • The echo will go to standard out, or >"$3" could be added to the line, making the output file the third parameter;
  • The echo command 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:

  1. Copy contents of file FileA.txt in column A of Excel sheet
  2. Copy contents of file FileB.txt in column B of Excel sheet
  3. Save Excel file as .txt file
  4. Open .txt file in Notepad++
  5. Replace TAB character with `.'

enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like