Need to add a series of spaces and a single digit at a specific character count to the end of every line
-
Hello,
I’ve got a txt doc with a few thousand lines of barcode data. The length of the data varies from line to line. I need to add a series of spaces, followed by a “1” at precisely the 35th character (from the margin) at the end of every line. Any thoughts as to the best way to do this? I have a ^ character that is at the end of every line for “find & replace” purposes, but the variable length situation is throwing me.Thanks in advance.
-
a simple python script would look like
position = 34 spacer = ' ' def add_text(content, line, total): pos=editor.positionFromLine(line) length=len(editor.getLine(line).strip()) if length <= position: editor.insertText(pos+length, '{0}1'.format(spacer*(position-length))) editor.forEachLine(add_text)
As long as the text in the line doesn’t exceed 34 chars it will fill the line with
spaces and then add the number 1.Cheers
Claudia -
Thanks Claudia - That looks like exactly the kind of thing I’m looking for.
When I run the script, it kicks a “NameError: name ‘editor’ is not defined.” How would I go about defining that in order for this to function? Forgive me, as I’m brand-spankin’-new to python…
-
I think that what you need to do is put the following line at the top of the python script:
from Npp import *
I have that line in my startup.py so that I don’t have to put it in every single python script I develop. Once executed (from any single script), it is good for the entire Notepad++ session (that’s why I found startup.py a good place for it).
-
Scott, afaik this is the default. I can’t remember adding this to startup.py and it is there.
@Justin-Tabor
I assume that the plugin installation failed. May I ask you how you did the install?
Via Plugin Manager? If so, please use the msi. You don’t have to revert
the plugin manager install, the msi will override it.Cheers
Claudia -
While the pythonscript (a plugin which I am a big BIG fan of!) works, I’m not a big fan of suggesting it as solutions to problems that might be solved another way. This is because not everybody is into pythonscript, and, if not using it for other purposes, might balk at the burden of installing it.
Here’s how I might solve your problem another way, with the big inference being that all of your original lines are LESS than 35 columns long (maybe this is easily assumed from your original problem statement).
Anyway, on line 1 I would add spaces so that you get to column 35. Then from that caret position, invoke the Column Editor (default keycombo: Alt+C) and insert some very-unique text (that won’t occur elsewhere in your document) using the “Text to insert” box. This will add a variable amount of spaces out to column 35 and then your very-unique text on each line. From there it is easy to do a Find+Replace on the very-unique text and replace it with your desired “1”.
-
I’m sure you are (as usual) correct about the “import”. Since Justin said nothing about Pythonscript failing to install, I was just trying to debug it from the point of view of the error he was getting.
-
I agree with your column editor solution - didn’t thought about it.
BUT why adding some unique text if he can add the needed 1 ;-)Cheers
Claudia -
Yes, very good point. At first I was going to suggest inserting number sequences (because that is what I typically do with the column editor) and then using a \d+$ regex to replace those with ‘1’, but then I noticed the “Text to insert” choice, and, well, I don’t know…I had my mind wrapped around having to do some sort of Find+Replace solution and…ugh! :-)
-
Dang - You guys are awesome! Scott’s method was the most painless. Thanks for all the help!
-
Hello, Claudia, Justin and Scott,
Justin, in addition to the Python script and the native N++ column editor feature, here is a regex S/R equivalent :-))
So, let’s suppose that we start with the original text, below :
BEGINNING This is a test A line with 25 characters an another try + a COMPLETE line of 34 characters_1 END
-
Open the Replace dialog
-
In the Find what : field, type :
(?-s)^(?!.{34}( |1))(.+)|^.{34}\K +
-
In the Replace with : field, type :
?2\2 1 , with 34 SPACES, between \2 and the digit 1, that should be ADDED
-
Select the Regular expression search mode
-
Click, TWICE, on the Replace All button ( IMPORTANT )
You should obtain the modified text, below :
BEGINNING 1 This is a test 1 A line with 25 characters 1 an another try 1 + 1 a COMPLETE line of 34 characters_11 END 1
NOTES :
-
The
(?-s)
syntax ensures that the dot (.
) will stand for standard characters, only ! -
The
^
is the assertion beginning of line, which must be verified -
The negative look-ahead (
(?!.{34}( |1))
) verifies if the condition NO space NOR the 1 digit, at position 35, is true. This case occurs, ONLY, on the original text, before any S/R -
So, the regex catches, first, all the standard characters (
.+
), of any non-empty line, stored in group 2 -
In replacement, as group 2 exists, it rewrites the current line, then adds a minimum of 34 spaces and the final digit 1
-
When you click a second time, on the Replace all button, the left part of the alternative cannot be achieved, as the look-ahead condition is, this time, false
-
So, the regex engine tries to match the right part of the alternative (
^.{34}\K +
) -
The first part (
^.{34}
) select the first 34 characters of each line -
Due to \K syntax, this range is forgotten and the regex engine, now, matches any consecutive non-null range of space characters (
+
), from the 35th position, included -
In replacement, as the group 2 is not used any more and that the conditional replacement,
?2...
, do not have an ELSE part, these space characters are simply deleted !
REMARKS :
-
The nice thing is that you may add, as many spaces, as you want, after the 34 first spaces, in the replacement field !
-
And, any additional click, after the second one, on the Replace all button, does not match anything else :-))
Cheers,
guy038
-