I am looking to replace the last 6 digits from a number with 6 random numbers. How do I do that in Notepad++ or TextCrawler Pro? I have a bunch of text files and I want to make the software scan for all the 9 digits combinations and replace the last 6 ones randomly.
13 Answers
Neither of the two programs appears to support variables in the "replacement" field (other than capture groups from the original matched text). So they cannot produce random replacements; at best, they can reorder the existing digits in some fixed manner (e.g. 123456789 → 123496857 every time).
However, I feel obligated to suggest a Perl oneliner instead:
perl -p -e 's/\b(\d{3})\d{6}\b/$1.sprintf("%06d", int(rand(1000000)))/ge'The regex \b(\d{3})\d{6}\b works the same as in Notepad++ (which uses PCRE); it matches 9 digits and captures the first 3 for later use.
In Perl's s///, the /e flag indicates that the replacement is to be interpreted as actual Perl code, so it can call functions and generate random numbers. You can also achieve the same in other languages which support a replacement callback (e.g. in Python or Ruby).
Usage:
perl -p -e '...' < file.txt > file_new.txt (create a new file)
perl -i -p -e '...' file.txt (update in-place) I am looking to replace the last 6 digits from a number with 6 random numbers.
You cannot do this directly in Notepad++.
However, you could install the python script plugin and write some code to do so.
Python Script for Notepad++
- Full programmatic access to Notepad++ features and menus
- Full programmatic access to all of Scintilla features
- Call other plugin menu items
- Assign menu items, shortcuts and toolbar icons to scripts
- Process Notepad++ and Scintilla events, direct from a Python script
- Python console built-in
- Full regular expression support for search and replace - script Python regular expression replaces
- Start external programs and pipe the output direct to a Notepad++ document, or filter it, or simply to the console window
- Full documentation for all the objects and methods
Source Python Script - Plugin for Notepad++
2You can run a python script within the PythonScript plugin.
If it is not yet installed, follow this guide
Create a script (Plugins >> PythonScript >> New Script)
Copy this code and save the file (for example random.py):
import re
def calculate(match): # generate a random integer in range 0-999999, cast to string and fill with 0 to the left return str(random.randint(0,999999)).zfill(6)
editor.rereplace(r'(?<=\b\d{3})\d{6}\b', calculate)- Open the file you want to change
- Run the script (Plugins >> PythonScript >> Scripts >> random)
- Done
Regex explanation:
(?<= # positive lookbehind, zero-length assertion that makes sure we have before \b # a word boundary \d{3} # 3 digits
) # end lookbehind
\d{6} # 6 digits
\b # word boundaryGiven:
abc 123456789 xyz
def 987654321 tuv
ghi 111111111 opq
klm 222222222 rst
123456789123465
111111111111111Result for given example:
abc 123535410 xyz
def 987865493 tuv
ghi 111414834 opq
klm 222382724 rst
123456789123465
111111111111111