0

I'm using Notapad++ and I have 2 situations here that I'm stuck with, regarding large text files that have lists of verb conjugation for my students, to use with a database of an android app.

Situation 1 There are text files where I want to switch between every 2 lines, so that line 2 goes first before line 1, and line 4 goes before line 3, and so on for the rest of the text document. I mean, instead of line 1,2,3,4,5,6 they would be 2,1,4,3,6,5,etc..

Situation 2 In other text files, I want to merge every 4 lines in one line, as follows: start with line 4, then add "-" sign, then the text of line 2, "-" sign, then the text of line 1, then a comma, then the text of line 3.

I'm sorry if that's confusing. Here is an example.

I need this:

Past participle
Past
Meaning
Verb

to all become one line like this:

verb - past - past participle,meaning

Snippet of the first file:

تعليمات
instructions
مهندس
engineer
أرضية
floor
والدين
parents

Snippet of the second file:

changed
changed
يتغير
change
relaxed
relaxed
يسترخى
relax
invited
invited
يدعو
invite

Many thanks in advance for your kind help.

  • If you can provide a snippet/example of your two files, that would help a lot in providing a working solution. I also believe this would be best solved either with a Notepad++ macro or a script or program, which can work on your text files, rather than applying regex. – Kristian Oct 19 '14 at 20:15
  • Sure. I've added 2 snippets above as you instructed me. Thanks. – Muhammad Oct 19 '14 at 21:27
  • Your question is much better and easier to read/understand now. – Kristian Oct 19 '14 at 21:59
0

Okay, I've managed to make this work now with macros in Notepad++.

Just for clarification, a macro allows you to repeat a set of actions again (and again...) but you will need to "teach" Notepad++ what it is you want to do first. So follow the instructions below closely or you could end up with a non-working result.

I can't figure out how (or if it is indeed possible) to export the macro into a format I can post here, so I'll go throught the method I used step-by-step.

Please note that for simplicity I will only explain how to work with macros for your first case (your first file) so as not to repeat myself unneccesarily. Also, please make sure you have a backup copy of the original file before you attempt this in case something does not work as intended.

Okay, here goes:

  1. Open up Notepad++, open up your file.

  2. Make sure you are at the beginning of your first line of data, that is, the first line with the first word in your dictionary in your case.

  3. From the "Macro" menu select "Start recording"

  4. Do what you need to do with only the first set of data. In the case of your first file, that would be something like:

    • mark the first line
    • cut it out with ctrl + X
    • move to the beginning of the next line
    • insert the line you cut out with ctrl + V
    • move down to the beginning of the next line

the last step here is important because you want to be at the 'right place' for the next run-through of your macro

  1. Now from the "Macro" menu select "Stop recording"

  2. From the "Macro" menu, select "Save the recorded macro", and give it some appropriate name and click "OK"

  3. Now (still having the cursor at the beginning of next line) select "Macro" > "Run a macro multiple times", select the macro you saved in step 6 and choose "Run to end of file", then click OK.

The macro will now work its way through your file. Be sure to give it a moment to finish if your file is large! You should now see that the macro has moved all your lines down as needed.

The same principle can be applied to your second request, although that requires a more advanced macro with more recorded actions. Once you understand the concept and know how to record a macro and play it back, however, it should be doable.

Hope this was helpful!

  • Worked like magic! both situations. Many thanks for introducing notepad++ macros to me. This was my first attempt, and thanks to your down-to-earth instructions, I find it way much easier than regex. You've opened a world of possibilities to me. I cannot thank you enough! – Muhammad Oct 19 '14 at 22:43
  • I'm glad I helped you learn something new. I learned something from the method @Vembu suggested, so I am happy as well. – Kristian Oct 19 '14 at 22:48
1

Situation2:

I believe you would like the interchange the lines, so that the line 2 should come first followed by line 1

We can achieve this in two steps:

Step 1:

First we can interchanged the values:

apple
ball
cat
doll

Find what :(.+)\r\n(.+)

Replace with :\2\r\n\1

output:

ball
apple
doll
cat

This will select the lines in the order of 2 and interchange it. you can increase this expression as follows

apple
ball
cat
doll

Find what :(.+)\r\n(.+)\r\n(.+)

Replace with :\3\r\n\1

output:

cat
ball
apple
doll

Above is to lock three lines and to replace 3rd and 1st line

Note: Please don't select the option "wrap around"

enter image description here

Step 2:

Now can merge the line with following expression

ball
apple
doll
cat

Find what :(.+)\r\n(.+)

Replace with :\1\2

ball apple
doll cat

Hope this helps!

  • Thanks so much Vembu for teaching me this. I really appreciate it. However, I found that macros, which Kristian introduced to me, is much simpler than regex, which I find too hard to understand, wheras macros a child's play compared to it. Thanks very much for your time. – Muhammad Oct 19 '14 at 22:39
  • I learned something from your answer, so upvoted it. So it wasn't in vain :) – Kristian Oct 19 '14 at 22:42
0

Try this:

Situation 1:

تعليمات
instructions
مهندس 
engineer
أرضية 
floor 

Hit Ctrl+H to access the Replace dialog, tick Regular expression, and enter the below expressions.

Find what : (.+)\r\n(.+)\r\n(.+)

Replace with: \1\t\2\t

Output:

تعليمات instructions
مهندس engineer
أرضية floor 

Situation 2:

Past participle
Past
Meaning
Verb

Hit Ctrl+H to access the Replace dialog, tick Regular expression, and enter the below expressions.

Find what :(.+)\r\n(.+)\r\n(.+)\r\n(.+)

Replace with:\1-\2-\3,\4

(you can interchange numbers as per your wish)

output:

Past participle-Past-Meaning,Verb

Hope this helps!

  • @Muhammad I would try with this method first (before the one I described using macros) – Kristian Oct 19 '14 at 21:56
  • @Verbu Many thanks. Your solution to the second situation worked great. I only had to change the replace with with: \4 - \2 - \1,\3 As for the first situation, this is not exactly what I'm trying to do. I need to switch lines, not merge them. Or better still, merge them so that the text of the second line is first followed by the text of the first line, with a comma in between the 2 texts. – Muhammad Oct 19 '14 at 22:02
  • @Muhammad. I have tried this in local test machine and it works fine. – vembutech Oct 19 '14 at 22:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.