Merge 2 text files into one, same lines - text

I have one file and contains:
file2.txt
PRIMERB
PrinceValiant
Priory
PRISTINA
embossed
heavy
incised
light
Outline
ribbon
and
file1.txt
PRIMERB 333
PrinceValiant 581
Priory789
PRISTINA3!1
embossed509
heavy5#
incised999
light5*1
Outline937
ribbon-81
I'd like to combine/merge these two files together so they would be like :
PRIMERB 333 PRIMERB
PrinceValiant 581 PrinceValiant
Priory789 Priory
PRISTINA3!1 PISTINA
embossed509 embossed
heavy5# heavy
incised999 incised
light5*1 light
Outline937 Outline
ribbon-81 ribbon
How would I do this in notepad++?

Add space characters to the end of the first line of file1 until it is longer than the longest line in file1.
Do a column mode selection of the entire contents of file 2. Do this by holding the ALT key down while dragging the mouse across the file. As you drag you should see a rectangular area of the screen selected. It may be easiest to start the selection before the first character in the first line of file2. Could also do a column mode selection with just the keyboard. Hold the ALT and Shift keys down while moving the cursor with the arrow keys.
Copy the selected text. (Control-C or menu => Edit => Copy or context menu => copy.)
Paste after the spaces added to file1.
Remove unnecessary spaces.
If the existing spaces in files1 and file2 are important you might use a regular expression to alter every line in file2 to have some character or character sequence that does not occur in either file before selecting its contents. For example, find ^ and replace with !!. Then you can use another regular expression to remove only the spaces added by the paste. For example, replace _*!! (space, asterisk, exclamation-mark, exclamation-mark) with _ (space; note that spaces would show incorrectly in these two strings, so they are shown as underscores _ for clarity).
See also the Editing => Column mode editing section of the Notepad++ help pages.

Maybe you can try ConyEdit. It is a cross-editor plugin for the text editors, including Notepad++.
Follow the steps below:
1, keep ConyEdit running.
2, use the cc.gl a command line to push data to array a.
3, use the cc.gl b command line to push data to array b.
4, use the cc.p command line to print the contents of the array a and array b.
Gif Example

Instead of finding some way of automating this, I think it'll be easier for you to just copy & paste...
But that purely depends on how many rows of text you got in those text files. If they contain less then 50 lines, I suggest you just copy (or cut) and paste.
I wouldn't know any way to automate that in Notepad++ anyway.
Edit:
After your request I wrote a quick PHP script that takes the lines from 'file1.txt' and 'file2.txt' and combines it to 'file3.txt'
<?php
$files1 = file('file1.txt'); // read file1.txt
$files2 = file('file2.txt'); // read file2.txt
// Assuming both files have equal amount of rows.
for($x = 0; $x < count($files1); $x++) {
$files1[$x] = str_replace(array("\n", "\r"), "", $files1[$x]);
$files3[$x] = $files1[$x]." ".$files2[$x];
}
$result = implode("", $files3); // combines the array to a single string.
if(file_put_contents('file3.txt', $result)) { // puts the imploded string into file3.txt
echo "Writing to file 'file3.txt' was successfull.";
}
?>
Now I would like to help you best I can, but I cannot access my own domain at this time, and I have not yet wrote something for you to upload your own files to it.
You can run this your own by downloading the latest USBWebserver
1. Extract the files from the .zip you downloaded from the USBWebserver website.
2. Go to the just extracted 'root' folder.
3. Delete everything inside that 'root' folder.
4. Copy the code above and save it as 'index.php' inside the 'root' folder (you can do this with notepad++ too).
5. Move your 'file1.txt' and 'file2.txt' to the same 'root' folder.
6. Go up one folder and execute 'usbwebserver.exe'.
7. Click on 'localhost' when the window pops up.
8. If you get the message: "Writing to file 'file3.txt' was successfull." you should now have 'file3.txt' in that 'root' folder.

Related

Collect markdown headlines in vim?

I am trying to come up with a TOC (table of contents) of a text document:
lines starting with # (hashtag) VIM would "collect" into the buffer or maybe place them at the TOP OF FILE.
(Or - other idea - erase all lines in a duplicate file NOT starting with a #)
There must be solutions to this question...
I think you are looking for the :global command.
You can see all lines starting with # with :g/^#/p
and put them in a file using the :redir command :
:redir > toc.txt
:g/^#/p
:redir END
Note that toc.txt will contain the line numbers if you have set number. There is also this post on the subject.
Alternatively, you can delete all lines not starting with # with :v or :g! and save it to another file.
:v/\#/d
:w! toc.txt | undo
Look also for :copy (:t) and :move.
:global applies the command you provide to each matched lines in the order they appear in the file, so :g/^#/t0 will copy headers to the top of the file but in reverse. We could do :v/^#/m$ to move all non-header lines to the end, leaving the headers on top but stripped from the original text. Another way is to place some marker to mark the end of the table of content and copy header lines one line above this marker :
:g/^#/t?end-toc?-1

How to copy (yank) from file 1 and paste (put) in file 2 in vi using buffer?

I want to know the process for copying data from file 1 and paste the data into file 2 in vi editor using a buffer.
Can you please tell me the step by step process.
How to do that?
From vi manual:
6.5.6.3 Using Named Buffers
To repeatedly insert a group of lines in various places within a document, you can yank (or delete) the lines
into a named buffer. You specify named buffers by preceding a command
with double quotes (") and a name for the buffer. For example, to yank
four lines into the named buffer a, type "a4yy. You can use several
different buffers. For example, you might also delete text from one
location and add it to several others. To delete 12 lines into the
named buffer b, type "b12dd.
To insert the text, precede the p or P command with n, where n is the
named buffer. For example, to insert the lines saved in buffer b, type
"bP.
You can overwrite named buffers with new lines. The buffers are saved
until you exit vi.
When you use named buffers, you can safely delete and yank other text
without affecting the lines you have already saved in the named
buffers -- unless, of course, you purposely overwrite the named
buffer.
Just in case you don't really need the buffer contents...
then you can also just directly do something like this to overwrite file2:
:4,12w file2
or append to it with something like:
:5,20w >> file2
Of course other standard ex line addressing options are also available.

less viewer: Copy all the lines to clipboard

There has already been a post in stackoverflow for VI editor for copying all the text into the clipboard. (Copy all the lines to clipboard) I want to do the same thing with the less viewer. I tried to search online for the process called "yank" and I did not find anything for it.
How do I copy all lines in the less editor into the clip board.
And I cannot close less and reopen it in vi. It is because of the fact that I have managed to load this file into the editor and while I have loaded it, the file has already been moved in the back end. It is a long story. The easiest solution for me now is to copy the contents of the file into memory.
less doesn't have a clipboard, but you may be able to get it to output what's stored in its buffers to a new file. This will only work if the entire contents of the file are buffered:
Type g to go to the top of the file
Type | (that's a pipe character, not an L or I) to indicate that you want to output to a pipe
Type $ to indicate that you want the output content to go to the end of the file
Type dd of=/path/to/new/file and press Enter
The dd command will take the piped data and save it to the file passed to the of= argument.
as an workaround you can set terminal's font size to 1, then select with mouse and copy (works for big , but not huge files).
If the file is not too big and if it fits in your terminal number of lines configured, then do the following:
Terminal > Edit > Clear to start
cat <file_name>
Terminal > Edit > Select all
Terminal > Edit > Copy

Put all lines that start with X at the end of the file with vi

What is the best way to take all the lines that begin with the word "define" and paste them at the end of a text file with vi?
For example, if I have:
define XXX
a
define YYY
b
and I want to get:
define XXX
a
define YYY
b
define XXX
define YYY
Solutions:
In vi, g/^define/t$ works nicely.
See below for vim solution.
You can use Go<ESC> (gee, owe, and the escape key) to open up a line at the end of the file, and then enter the characters:
!!grep '^define' %<ENTER>
That replaces your current line (the one you just opened at the end of the file) with the output of that grep command (where % is the current file).
You need to ensure the file is saved first since it uses the on-disk copy, not the in-memory copy and keep in mind this is with Vim - older variants of Vi may not have this facility.

Sorting Vim Folds

I have a file that looks something like this:
dog{{{
blah blah blah
}}}
cat{{{
blah blah
}}}
aardvark{{{
blah blah blah blah
}}}
In my vimrc I have set foldmethod=marker, so that the contents of the file are all folded at the curly braces. My question is, how can I sort the file based on the fold header? I want the aarvaark section to come first, then cat, then dog.
Some folded sections have other folds within them. I do not want to sort those lower fold levels. The file should be sorted by level 1 folds only.
With all folds closed (zM or :set foldlevel=0), you can use delete dd and paste p to manually shift the folded blocks around. This is okay for small amounts of text.
For huge texts, you'd have to write a sort function yourself, as Vim does not offer such functionality. The following algorithm can be used:
Join all folded lines together with a delimiter that does not exist in the text (e.g. <Nul>).
Execute the :sort command.
Un-join the range on the special delimiter.
With error handling and corner cases, it's actually quite a bit of implementation effort. I've made an attempt here: https://gist.github.com/4145501
One approach would be as follows:
Search and replace the line endings so that the level-one folds are each on one line (i.e. replacing the line ending with a unique string that doesn't appear elsewhere in the file).
Use :sort to sort the lines in the file, as usual.
Reverse the search and replace from Step 1 to reintroduce the line endings.
This could all be recorded as a Vim macro.
Steps 1 and 3 would be straightforward if there weren't nested folds, or if there was an easy way of distinguishing top-level folds from nested ones (e.g. if the level-one {{{ and }}} lines are flush left and the others have leading space). If this isn't the case, it's probably still possible but much more difficult.
I would use power of Vim macros.
Suppose that you do not have any other staff in your file except the records and first record starts on the first line. And that you do not have start of level 1 and end on the same line. And all your {} match well.
Go to the first entry and record a macro to register a (qa - start recording q - stop)
/{^MV%k:s/\n/EEEEEE/^Mj^
Copy mine or better record it yourself: (^M - stands for literal enter)
/{^M find next {
V%k select line-vise till the end of level 1 but not the closing line with }}}
:s/\n/EEEEEE/^M change symbol 'end of line' to some unique string inside selection
j^ go one line down and to the begging of the line in case the previous record had leading spaces.
Run the macro starting from the second line (suppose that the first is processed already when you recorded the macro.)
In normal mode 100000#a - use whatever number greater than number of your records. Macro will stop itself when all lines are processed if the first record is in the first line of file. As a side effect it will visually select the first line - unselect it.
Sort the file
:sort
Restore the end of lines:
:%s/EEEEEE/^M/g
Note ^M should be literal enter. To type it use Ctrl-V Enter
It works with nested levels because % jumps to the matching closing }. That is why it is important that you have all the {} matched well.
Macro will stop when could not go up with k that is why you must have the first record on the first line. Empty lines or anything that is not a record between records will be ignored by the macro.
The method above should work well for your file. If you want a more general approach with folds then use the following commands in the macro instead of /{ and %
Firstly open all folds
zj - go to the start of the next open fold
]z - go to the end of the current fold.
The rest of the macro is the same.
This method is more general but I found it less reliable since it depend on all folds open, folding enable and so on.
There's a vim-sort-folds plugin that looks specially made for this.

Resources