14

I have a long text file

gallery-dl -g -i w4b027.txt >
gallery-dl -g -i a4b028.txt >
gallery-dl -g -i b4b029.txt >
gallery-dl -g -i c4b030.txt >
gallery-dl -g -i d4b031.txt >
gallery-dl -g -i w4b032.txt >
gallery-dl -g -i w4b033.txt >
gallery-dl -g -i w4b034.txt >
gallery-dl -g -i w4b035.txt >
gallery-dl -g -i w4b036.txt >
gallery-dl -g -i w4b037.txt >
gallery-dl -g -i w4b038.txt >
gallery-dl -g -i w4b039.txt >
gallery-dl -g -i w4b040.txt >

I want to make it

gallery-dl -g -i a4b027.txt > a4b027x.txt
gallery-dl -g -i b4b028.txt > b4b028x.txt
gallery-dl -g -i c4b029.txt > ...

the first text file to the second text file with suffix "x".

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Roxion
  • 344
  • 1
  • 2
  • 11

5 Answers5

45

You can hold alt to select the file names in block mode. Then copy and paste them to the desired location and then modify all rows at once.

It's easier to describe with a gif than with words:

Notepadd++ block mode copy and paste

Instead of using the mouse, you can also hold shift+alt and then use the arrow keys to select in block mode. You can also use the page up/down keys to quickly select whole columns in larger files.

kapex
  • 995
  • 2
  • 12
  • 20
  • 5
    This will work only if the filenames have the same length. – Toto Feb 14 '21 at 15:25
  • 7
    For someone unfamiliar with regular expression, I suspect this method will cause less problems... – Nelson Feb 14 '21 at 17:22
  • 1
    Upvote. This is much more versatile solution than the complex replace functions in other answers – Jan Doggen Feb 14 '21 at 20:58
  • 1
    @Toto this works with variable length lines too because you can make the alt+selection way to the right of everything. It doesn't have to be immediately on the last character of the line. You'll end up with a bunch of white space which you can find and replace but that is easy. – Brad Feb 15 '21 at 15:35
  • 2
    @Brad: You can't add the `x` before the extension if the filenames haven't the same length. – Toto Feb 15 '21 at 16:46
  • @Toto, that's true here. But that's the easy part because you can replace ".txt" with "x.txt" in your selection using the same alt+click technique. – Brad Feb 15 '21 at 17:30
  • 1
    @Brad: No, you can't when the `.txt` are not all in the same column. – Toto Feb 15 '21 at 17:45
  • @Toto, ah you're right, `In Selection` is disable with alt+select. But then you can paste into another file, do your change then past back into the original file. The gist of this answer still stands. – Brad Feb 15 '21 at 17:50
  • 1
    @Brad: Then it seems to me that a single regex solution is better than multiple manipulations; – Toto Feb 15 '21 at 17:51
  • @toto I'd still favor an accessible non regex answer – Brad Feb 15 '21 at 18:31
  • 1
    I agree it's not a universal solution. It's versatile in the sense that you don't have to care about the actual content - it's only important that the parts which you want to copy/modify are aligned in columns. I think detecting visual patterns like in this case is much easier than having to come up with a regex patterns. For more complex issues I would use regex too, but it's always good to have another tool for certain situations. – kapex Feb 15 '21 at 23:27
  • 1
    If OP is not knowing regex, this answer can help them in the future, while the regex answers are probably very specific to this issue... – Laurent S. Feb 16 '21 at 16:06
22
  • Ctrl+H
  • Find what: ^.+\h(\S+)(\.txt) >\K
  • Replace with: $1x$2
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

^           # beginning of line
  .+          # 1 or more any character but newline
  \h          # horizontal space
  (\S+)       # group 1, 1 or more non-space character
  (\.txt)     # group 2, extension .txt
   >          # a space and > character
  \K          # forget all we have seen until this position

Replacement:

 $1         # a space and content of group 1 (filename)
x           # letter x
$2          # content of group 2 (extension)

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Toto
  • 17,001
  • 56
  • 30
  • 41
  • May I know why this has been downvoted? – Toto Feb 15 '21 at 09:39
  • 2
    Not the down-voter, but when you try and show someone a regular expression, you should use the least amount of "magic" as possible. [This answer](https://superuser.com/a/1625799/395057) does a great job of using match characters only where needed and is much easier to read (but they should have explained what little regex they used, and theirs is actually wrong... I'm just pointing out the simplicity). I use regex all the time, and I have no idea what \K does or why its needed here. – JPhi1618 Feb 15 '21 at 15:02
  • @JPhi1618: 1) `\K` is the reset operator, as said in my answer it is used to forget all that was match before, there is no magic here. 2) The other answer is wrong, it doesn't produce the expected result. – Toto Feb 15 '21 at 16:44
  • I explained the other answer was wrong. And I still don't know what \K is - that description doesn't mean anything to me. I'm using the word "magic" to talk about regex code in general because to someone that doesn't use regex at all, it looks like random magic characters. Anyway, the point isn't what \K is for, the point is your regex is more complicated than it needs to be. As usual, readability is king. – JPhi1618 Feb 15 '21 at 16:51
  • 1
    @JPhi1618: I prefer effciency than readability, and this regex is not complex. If you want to see a really complex regex, have a look at [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html). – Toto Feb 15 '21 at 17:50
  • 2
    @Toto why do we need to "forget all we have seen until this position" at the very end of the regex? What problem with the rest of the regex is this \K solving? – Hellion Feb 15 '21 at 23:40
  • @Hellion: `\K` acts as a lookbehind, it allows to avoid useless capture groups and and the need of repeating the part of the text that doesn't change before and after the replacement. The replacement part is, then, really concise (i.e. ` $1x$2`), no needs to repeat original text `gallery-dl -g -i w4b027.txt >` – Toto Feb 16 '21 at 08:54
  • 1
    I agree that the regex in the answer is overly complex. `(\S+)\.txt >` -> `$1.txt > $1x.txt` Uses way fewer regex operations and does the job just fine. `\K` is highly nonstandard and barely gains anything here and `\h` doesn't gain anything compared to a simple space. – Chronial Feb 16 '21 at 16:06
  • @Chronial: What do you mean with complex, it's just a point of view. For example you solution uses more characters than I (find & replace). `\K` is PCRE standard and avoid to repeat the part that doesn't change. `\h` includes tabulation and is mandatory if you're using `x` flag that allows comments inside the regex. – Toto Feb 16 '21 at 16:41
  • @Toto by "more complex" he probably means, the other expression used some literal matching, a capture group, a wildcard, and a character-group operator: 4 well-known and widely understood pieces of regex. You used start-of-line operator, 3 wildcards, 2 character-group operators, 2 capture groups, some literal matching (including an escaped character), and the look-behind-discard operator: 10 or 11 pieces of regex, at least one of which many of us have never used or even heard of. Both regexes accomplish the same task. – Hellion Feb 16 '21 at 21:33
  • @Toto on the other hand I did learn something about the \K operator (though I still had to try it out for myself to see what it did here), so that was nice. – Hellion Feb 16 '21 at 21:34
19

Another way to simplify repetitive tasks with Notepad++ is with the Macro feature.

The below steps will use keyboard keystrokes which the macro will repeat for you...

  1. Open the long text file and put the mouse cursor to the very beginning far left of the first line

    enter image description here

  2. Select Macro | Start Recording from the Notepad++ upper menu bar

    enter image description here

This is where you start the keyboard steps to complete the task for the first line

  1. Hold down on Ctrl and press the key 7 times

  2. Hold down on Ctrl+Shift and press the key 3 times

  3. Press Ctrl+C

  4. Press the End key 1 time

  5. Press Ctrl+V

  6. Hold down on Ctrl and press the key 2 times

  7. Press the X key 1 time

  8. Press the key 1 time

  9. Press the Home key 1 time

This is where you stop the keyboard steps that completed the task for the first line

  1. Select Macro | Stop Recording from the Notepad++ upper menu bar enter image description here

You now have the macro built so you can start it and play it to the end of the file to perform those actions until the last line and it'll take care of the repetition for you.

So just stay on the second line of your document after pressing the number 11 Home key and...

  1. Select Macro | Run a Macros Multi Times... from the Notepad++ upper menu bar

    enter image description here

  2. Check Run until the end of the file and then press Run

    enter image description here

Output Results

gallery-dl -g -i w4b027.txt > w4b027x.txt
gallery-dl -g -i a4b028.txt > a4b028x.txt
gallery-dl -g -i b4b029.txt > b4b029x.txt
gallery-dl -g -i c4b030.txt > c4b030x.txt
gallery-dl -g -i d4b031.txt > d4b031x.txt
gallery-dl -g -i w4b032.txt > w4b032x.txt
gallery-dl -g -i w4b033.txt > w4b033x.txt
gallery-dl -g -i w4b034.txt > w4b034x.txt
gallery-dl -g -i w4b035.txt > w4b035x.txt
gallery-dl -g -i w4b036.txt > w4b036x.txt
gallery-dl -g -i w4b037.txt > w4b037x.txt
gallery-dl -g -i w4b038.txt > w4b038x.txt
gallery-dl -g -i w4b039.txt > w4b039x.txt
gallery-dl -g -i w4b040.txt > w4b040x.txt

Supporting Resources

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
  • @Roxion ... Here's a different approach I just added for a different and simple Notepad++ feature you can use. This doesn't require regular expressions at all and it helps make repetitive tasks with text documents more simplified too. – Vomit IT - Chunky Mess Style Feb 14 '21 at 02:40
  • 3
    Upvote. This is easier, more versatile, and less error prone than the regex replace functions in other answers. Once you master keyboard macro possibilities (including 'repeat till end of file') you can do so much with Notepad++ ... – Jan Doggen Feb 14 '21 at 20:57
  • slightly different (and better) keystroke sequence than I was going to post. +1 – Kevin Feb 16 '21 at 16:49
11

Notepad++ search and replace:

Find what: gallery-dl -g -i (\w+).txt >
Replace with: gallery-dl -g -i \1.txt > \1x.txt

It will need to be modified if not all the lines terminate with a blank.

enter image description here

harrymc
  • 455,459
  • 31
  • 526
  • 924
1

Another solution is to use Excel (or any other spreadsheet of your choosing). This may take a few more steps but it supports filenames of varying lengths, so it's more versatile. (You can do lots of other similar operations this way, so it's a useful technique to have in your toolkit even if some of the other suggestions are more expedient in this specific case).

  • Copy your text into columns (use the Import Wizard to delineate by spaces and in this case also "."s).
  • Insert a column between your filename and the column containing "txt", and fill it with "."s. Also add columns everywhere you want a space and fill them each with a single space, or you'll lose your spaces later on.
  • Copy the three columns containing filename, ".", and "txt" and paste them to the right of your ">" column. (Add another column of spaces in between.)
  • Insert another column between filename and ".", and fill it with "x"s.
  • Copy the entire table and paste it back into Notepad++.
  • Do a Find/Replace, make sure you are in "Extended" mode, and replace all "\t" with nothing to delete all the tabs.
Darrel Hoffman
  • 257
  • 2
  • 5
  • 12