Merge files line by line

0

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.

Kard Nails

Posted 2018-05-15T09:59:41.823

Reputation: 776

I would have used same approach. Where did you get 256 character limit? – Sandeep – 2018-05-15T10:06:46.843

@Sandeep When using the Flash Fill function in Excel I get a message that I should revise all the auto merges, as some of the content was clipped due to the 256 char limit. – Kard Nails – 2018-05-15T10:29:36.520

2I would copy contents of FileA.txt in column A of excel and contents of FileB.txt in column B. Then Save As .txt file. Open this .txt file in Notepad++ and replace TAB character between values with .. – Sandeep – 2018-05-15T10:40:33.733

@Sandeep That was simpler than I imagined. Thank you. If you post an answer, I'll accept it. – Kard Nails – 2018-05-16T06:28:09.827

I have added my answer. – Sandeep – 2018-05-16T08:47:30.670

Answers

3

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.

kenorb

Posted 2018-05-15T09:59:41.823

Reputation: 16 795

I'd forgotten about paste, which is the obvious solution, though in the example in the question there is no separator between the lines of each file: for that you need paste -d "" FileA.txt FileB.txt. – AFH – 2018-05-16T09:30:24.990

How would I go about creating a new file directly from the output? It's a long file... – Kard Nails – 2018-05-16T14:55:54.760

Just redirect to a new file, like: > new_file.txt. – kenorb – 2018-05-16T15:04:38.890

1

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.

JoeSlav

Posted 2018-05-15T09:59:41.823

Reputation: 142

I'm sorry, but what? What is diff and what is this one liner? As I said, my knowledge is quite limited. – Kard Nails – 2018-05-15T10:31:02.627

Tried to make it more explanatory. – JoeSlav – 2018-05-15T10:34:39.540

1

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.

AFH

Posted 2018-05-15T09:59:41.823

Reputation: 15 470

1

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.

kenorb

Posted 2018-05-15T09:59:41.823

Reputation: 16 795

1

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

RProgram

Posted 2018-05-15T09:59:41.823

Reputation: 475

0

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

Sandeep

Posted 2018-05-15T09:59:41.823

Reputation: 939