Home > Mobile >  Merge 2 files in Notepad or Vim
Merge 2 files in Notepad or Vim

Time:05-17

I have 2 files with me.

  • File1.txt:

    Bus
    Railway
    Cinema
    Motor
    
  • File2.txt:

    Stand
    Station
    Theatre
    Cycle
    

Above 2 files I have with me and I need output like below.

  • Output.txt:

    Bus Stand
    Railway Station
    Cinema Theatre
    Motor Cycle
    

I have thousands of lines like this and I can't do this one by one manually. Someone please help me to automate this.

I have Notepad and Vim ( i can't Install any other apps, because it's Office System )

CodePudding user response:

You can do it using vim macros. First, get your file into the following state:

Bus Railway Cinema Motor
Stand Station Theatre Cycle

Then, with your cursor on the first line (in normal mode) do the following key combo

qq0f<Space>r<Enter>jf<Space>r<Enter>2kddp@qq

Where <Space> is an actual space, and <Enter> is an actual enter key. You have now recorded a recursive vim macro. Your file should look like this

Bus
Stand
Railway Cinema Motor
Station Theatre Cycle

With your cursor on the third line, after which, you hit @q, giving you

Bus
Stand
Railway
Station
Cinema
Theatre
Motor
Cycle

Then do vipJ to join all lines in the paragraph.

Edit 1

Upon seeing the revised question, I propose the following: Use the command line. Others have pointed out the use of a few unix utilities that can make the job easy.

If that is beyond the constraints of your machine, you can do the following in vim:

First, get your output.txt into this format:

Bus
Railway
Cinema
Motor

Stand
Station
Theatre
Cycle

Then record the following macro to use on subsequent output files.

qqgg}j<Ctrl>vGI<Space><Esc>gg<Ctrl>v}k$xdiph"0P:%s/\s\ /<Space>/g

Which should solve your first instance. Then for your next instance, simply get the file into the format as previously shown and do @q to call the macro.

NOTE: The above only works when the entire file only contains two paragraphs to be merged.

A full explanation might be cumbersome and may ultimately be inaccurate, please use vim's inbuilt help texts to get it straight from the horse's mouth.

With regards to the files and how to do it a thousand times, I believe it is best to raise another question with your constraints as well as the file formats present, and the output required.

CodePudding user response:

Opening the files:

vim -o file1.txt file2.txt

Jump to the second one by pressing Ctrlww, now copy the whole file:

:%y

Jump back to the first one Ctrlww, now we are going to make the yank register 0 blockwise:

:call setreg('0',getreg('0'), 'b')

Start insert adding five or more spaces then Esc and finally

"0p
:%s/\s\ / /g

Another vimish solution:

Open the files:

 vim -o file1.txt file2.txt

copy this function to the clipboard

function! Join_files()
    wincmd j
    %y
    wincmd k
    call setreg('0', getreg('0'), 'b')
    normal gg
    .s/$/     /
    normal $"0p
    %s/\s\ / /g
endfunction

The next command loads the function to the vim memory

:@ 

Now run the function

:call Join_files()

Explaining the function:

wincmd j .............................. jump to the other file
%y .................................... copy its content
wincmd k .............................. jump back
call setreg('0', getreg('0'), 'b') .... make yank register blockwise
normal gg .............................. jump to the line 1
.s/$/     / ............................ add five spaces
normal $"0p ............................ jump to end of line and paste yank register
%s/\s\ / /g ............................. remove two or more spaces
  • Related