How to find copy and replace "quoted text" from one text file to another
-
Terry-R
Alright Terry, here it is:001 "name" : "EC-101 standard magazine", 002 "name" : "AK-101 30-round polymer magazine", 003 "name" : "EC-101 tactical magazine", 004 "name" : "AK-101 Circle 10 30-round magazine", 005 "name" : "EC series short barrel", 006 "name" : "AK series standard barrel", 007 "name" : "EC series long barrel", 008 "name" : "AK series long barrel", 009 "name" : "EC Tacto SAVV Pistol grip", 010 "name" : "AK FAB Defense AG-43 pistol grip (black)", 011 "name" : "EC Tacto SAVV Pistol grip desert", 012 "name" : "AK FAB Defence AG-43 pistol grip (Flat Dark Earth)", 013 "name" : "EC standard pistol grip", 014 "name" : "AK standard wood pistol grip", 015 "name" : "EC standard pistol grip", 016 "name" : "AK standard polymer pistol grip",
This is step #8, I am trying to get to step #9, which would merge the 2nd line of each pair into position in the first line.
This is tab #3 btw. -
@DankiestCitra
Thanks for the examples.This regex should work (provided data copied perfectly into code block)
Find What:
(?-s)^(\d+).+\R\d+(.+\R)
Replace With:${1}${2}
Just so you know what it is doing. It keeps (stores) the first line “line number” as that will be used to place it back into the correct position later on. It then reads over the remainder of that line and the number at the start of the second line (of each pair). Then it reads the remainder of the second line, storing that.
The replace field writes the 2 stored values back.Hope that helps.
TerryPS forgot to add, make sure the last line contains nothing, so in effect add a blank line. this is necessary as the regex expects a CR and/or LF after EVERY line.
-
@Terry-R
But this simply deleted the original ones. What is the point of that? -
@DankiestCitra said in How to find copy and replace "quoted text" from one text file to another:
But this simply deleted the original ones
Nope, you said move merge the 2nd line of each pair into position in the first line. That’s what the regex does. So you will be left with the original line number together with the new data. Isn’t that what you want? Then that tab#3 data can be reinserted into the original file, resorted and the line numbering removed.
Terry
-
@Terry-R
I’m sorry, I know you are probably annoyed a lot by me right now, but this method has confused the hell out of me.
Your regex really did delete every other line, honestly. I am sure I didnt mess it up, its just a copy/paste.I did some digging, found a different regex, and now I use this modified version of it:
Find: (?-s)^(.+)\R(.+)\R
Replace: \1\2\r\nThis successfully merged 2 lines together, every other line.
I will actually go for mkupper’s solution as this is closest to what I really want to do. -
@DankiestCitra said in How to find copy and replace "quoted text" from one text file to another:
I know you are probably annoyed a lot by me right now, but this method has confused the hell out of me.
No, not annoyed but it does seem as if you didn’t understand the concept. When I used your examples and my regex I get this:
which is what I think you need. There is no need to keep both sets of data as you are only interested in re-inserting this updated data back into the original file. The other file was only a donor and doesn’t need to be kept. or at least if kept then make sure you are working on a copy which you can edit without affecting the real files.
But if you can cobble together a process that makes you feel happy and seems to work for you, then good.
Terry
-
@Terry-R
But why do this long process just to keep the data that I need to insert? I can already do that easily by Marking, bookmarking and Copy/pasting the bookmarked lines.
Then I can manipulate the beginning and end of the text as I see fit, to add/remove spaces, characters at the end, etc.But you are correct in that I didn’t actually understand what your regex was supposed to do, I misunderstood completely, and I apologize for that.
In any case, your solution did help me, because it taught me how to separate the lines into alternating pairs, numbered appropriately, and now I successfully used @mkupper 's solution, together with your steps 1 through 8 (in the other thread), which are fairly straightforward and easy, I formatted the text I wanted into the format that mkupper showed me, and this works and is actually faster and better, with fewer mistakes possible.
Thank you very much @mkupper and @Terry-R for your input, I do realize I am a complete noob, but I did learn some things in the process.
P.S. The solution ended up being quite complicated for me, with more than 16 total steps to complete, but now that I learned it, it just seems easy.
-
@DankiestCitra said in How to find copy and replace "quoted text" from one text file to another:
But why do this long process just to keep the data that I need to insert?
I couldn’t understand that statement until I looked again at my original solution in #16287.
So I think I now see some of your confusion. You tried to follow the steps in #16287, but as I had alluded to earlier I was going to redo it for your requirement. The original solution relied on the data being dissimilar enough that the pairs would align correctly. In the end you just wanted a regex and I complied.
What I should have done was to redo that solution for you. In essence what we need to do for both files is:
- add line numbers to all lines starting from 1 onwards
- bookmark and then cut and paste in another tab the “name” lines
- redo line numbering but this time one set will have odd numbers and the 2nd set will have even numbers. this might mean they are pasted into different tabs first, given new numbers and then combined into 1 tab.
- sort lines based on their line numbering. This combines them so new data is alongside the old data.
- use a regex (as I showed earlier) to combine the 2 lines so that the “original” line number now has the new data. this also removes the 2nd line numbering added in step #3.
- Re-insert the combined data into the original file and re-sort based on line numbers.
- remove the line numbers.
The “donor” file which had the new data was being edited in the process, so that if needed in it’s original state would have meant a copy would be required for this process. The recipient file is being edited, on purpose and can be either a copy (generally the best idea especially when running a new process through the first few times), or the original if the process is tried and true. If a copy then it’s copied over top of the original after spot checking the result.
Hopefully that makes it a bit clearer.
Terry
-
@Terry-R
I already did 1 through 7 basically as you described here, except I do step 7 before inserting the combined data into the original file I want to modify.Everything works smoothly, I did some very very minor adjustments upon mkupper’s regex (for my needs I mean), and even wrote a guide for myself, because I am bound to forget things, so…yeah.
I am not quite sure what you mean about the “donor” file and the “original” here, I am always modifying copies, the original files are always untouched.
Oh and, with this solution now, I simply run the recorded Macro and select Run Macro Multiple Times > Run until the end of the file.
It takes about 1.5 seconds to complete it and go through several thousand lines of code in total :)
It is super fast, and does exactly what I want.In any case, thank you and @mkupper once again, for providing the solution to me, albeit with some slight needed modifications, but still, thanks a lot to both of ya.
-
I’m glad the OP was able to find a solution, and I appreciate the spirit of patience, exploration and iterative improvement that they and everyone else involved in the discussion exhibited throughout this whole process. I have nothing but good things to say about the OP’s attitude, but I still want to make this observation in case they are interested in further improving upon the solution:
While 1.5 seconds to process several thousand lines of code may seem fast, the script I posted above processes several hundred thousand lines of JSON in less than a second.
I am saying this because I believe that the OP has the personality traits of a successful programmer, and it is absolutely within their power, if they believe it is worthwhile, to learn a scripting solution that would provide better performance.
-