Replace numbers between quotes
-
I am trying to update a file that has over 2,500 lines of code that I must update the sequence numbers to. All sequence numbers below 5490 must be updated to be higher than 7400.
For example, I am trying to replace all numbers inside of " " like below.
Original code:
Seq=“3419”Desired output\replacement:
Seq=“7400”And the next replacement would be 7401 and so on. Is there a way to do this in the replacement tool or in Notepadd++? If I have to do it one by one that is fine as well.
-
@ivantiguy said in Replace numbers between quotes:
Is there a way to do this in the replacement tool or in Notepadd++?
There are a number of ways. All will involve some steps or learning (if going the PythonScript method).
Regex (Regular Expressions) are a method of finding and replacing text but unfortunately they have no built-in ability to replace with increasing numbers (as you require).
I propose a method which can use regex and some other tools within Notepad++. Before I could give you specifics though we need to see a sample of the actual file. Currently you did NOT include the samples within a code block and therefore what is represented in the post may not be exactly what you have (is code indented? does it include smart quotes amongst others?).
To show the example properly please read the FAQ post here. In particular how to insert examples, such as selecting them and clicking on the </> icon to set a code block around the example.
To outline the method:
- Number all lines
- Bookmark those lines with the sequence number to be changed using a regex
- Cut those and import into a new tab
- Insert an increasing number in the left margin, 7400 etc
- Use regex to replace the existing number with the new number in left margin
- copy all lines in the new tab back to the original file/tab
- sort using the line numbering given in step #1
- remove the line numbering using regex or block delete method
Terry
PS, I mentioned PythonScript as a method. We have a FAQ post here which may be of use to you if you decide on that method. You would start with the REPLACE WITH A SIMPLE COUNTER (i.e., RENUMBERING) code and it would need some changes to suit your need.
-
@Terry-R Here is an example of the code taken directly from the file.
Original Data:
<Locations Seq="3396" DbRevision="670"> <Updates> <Location FRS_ID="30CD802CC0A744248253F1F869AADD8A" Frs_CompositeContract_Entity_Id="bdc85ad4-9e4d-e311-ba73-005056a60016" DisplayName="Canada"> <Updates> <ivnt_FullName> <First> - Canada</First> <Second>DbNull</Second> </ivnt_FullName> </Updates> </Location> </Updates> </Locations> <Locations Seq="3397" DbRevision="670"> <Updates> <Location FRS_ID="B33153FEF4014CC6BAC40581564BC5E9" Frs_CompositeContract_Entity_Id="9d7073c2-9e4d-e311-ba73-005056a60016" DisplayName="Central"> <Updates> <ivnt_FullName> <First> - Central</First> <Second>DbNull</Second> </ivnt_FullName> </Updates> </Location> </Updates> </Locations>
What data should look like after:
<Locations Seq="7400" DbRevision="670"> <Updates> <Location FRS_ID="30CD802CC0A744248253F1F869AADD8A" Frs_CompositeContract_Entity_Id="bdc85ad4-9e4d-e311-ba73-005056a60016" DisplayName="Canada"> <Updates> <ivnt_FullName> <First> - Canada</First> <Second>DbNull</Second> </ivnt_FullName> </Updates> </Location> </Updates> </Locations> <Locations Seq="7401" DbRevision="670"> <Updates> <Location FRS_ID="B33153FEF4014CC6BAC40581564BC5E9" Frs_CompositeContract_Entity_Id="9d7073c2-9e4d-e311-ba73-005056a60016" DisplayName="Central"> <Updates> <ivnt_FullName> <First> - Central</First> <Second>DbNull</Second> </ivnt_FullName> </Updates> </Location> </Updates> </Locations>
-
@ivantiguy Here is what I would do:
Click at the beginning of the first line. Scroll to the bottom and Shift+Alt+click at the beginning of the last line. That will give you a zero-width rectangular selection at the left edge of the file comprising all the lines.
Use Edit | Column Editor… and select Number to Insert, set Initial number, Increase by and Repeat to 1, and set Leading: Zeros. Then click OK. That will insert a line number at the beginning of every line, which you’ll need in a later step.
Now make another zero-width selection in the same way as the first step, but this time select between the end of the line numbers and the beginning of the “real” text in each line. Be sure all lines are included.
Now use Edit | Line Operations | Sort Lines Lexicographically Ascending.
Scan through the file until you find the place where all the lines that start
<Locations Seq="
are grouped together.Make a rectangular selection encompassing the sequence numbers you want to change by clicking to the left of the first digit in the first of the lines with sequence numbers and then Shift+Alt+clicking to the right of the last digit in the last of the lines with sequence numbers. Be sure you include exactly the numbers, nothing more and nothing less.
Now use Edit | Column Editor… again, but this time set the initial number to the new first number you want.
Now click anywhere (to remove the column selection) and use Edit | Line Operations | Sort Lines Lexicographically Ascending to put the lines back in order.
Finally, make a rectangular selection encompassing the line numbers and use the delete key to remove them all.
That sounds like a lot of steps, but they’re all pretty simple, and don’t require any programming or debugging, as you can see at each step what is happening. The disadvantage is that this only works if your lines are formatted consistently, as in your example. If some lines have different spacing or other variations that would cause the sequence numbers of interest not to sort together or not to line up in a column, a more complex approach would be required.
-
Hello, @ivantiguy, @terry-R, @Coises and All,
Here is the way I would choose to achieve your goal :
- From your present INPUT text :
<Locations Seq="3396" DbRevision="670"> <Updates> <Location FRS_ID="30CD802CC0A744248253F1F869AADD8A" Frs_CompositeContract_Entity_Id="bdc85ad4-9e4d-e311-ba73-005056a60016" DisplayName="Canada"> <Updates> <ivnt_FullName> <First> - Canada</First> <Second>DbNull</Second> </ivnt_FullName> </Updates> </Location> </Updates> </Locations> <Locations Seq="3397" DbRevision="670"> <Updates> <Location FRS_ID="B33153FEF4014CC6BAC40581564BC5E9" Frs_CompositeContract_Entity_Id="9d7073c2-9e4d-e311-ba73-005056a60016" DisplayName="Central"> <Updates> <ivnt_FullName> <First> - Central</First> <Second>DbNull</Second> </ivnt_FullName> </Updates> </Location> </Updates> </Locations>
With the following regex S/R :
SEARCH
\R(?!\h*<Locations)(?!\R)(?!\z)
REPLACE;
We get the temporary text, below :
<Locations Seq="3396" DbRevision="670">; <Updates>; <Location FRS_ID="30CD802CC0A744248253F1F869AADD8A" Frs_CompositeContract_Entity_Id="bdc85ad4-9e4d-e311-ba73-005056a60016" DisplayName="Canada">; <Updates>; <ivnt_FullName>; <First> - Canada</First>; <Second>DbNull</Second>; </ivnt_FullName>; </Updates>; </Location>; </Updates>;</Locations> <Locations Seq="3397" DbRevision="670">; <Updates>; <Location FRS_ID="B33153FEF4014CC6BAC40581564BC5E9" Frs_CompositeContract_Entity_Id="9d7073c2-9e4d-e311-ba73-005056a60016" DisplayName="Central">; <Updates>; <ivnt_FullName>; <First> - Central</First>; <Second>DbNull</Second>; </ivnt_FullName>; </Updates>; </Location>; </Updates>;</Locations>
-
Do a column mode selection of all the
Seq
numbers : -
Open the Column Editor (
Alt + C
)-
Select the Number to Insert option
-
Initial Number
7400
-
Increase by
1
-
Repeat
1
-
Leading
None
-
Format
Dec
-
Click on the
OK
button
-
=> The temporary text is changed as :
<Locations Seq="7400" DbRevision="670">; <Updates>; <Location FRS_ID="30CD802CC0A744248253F1F869AADD8A" Frs_CompositeContract_Entity_Id="bdc85ad4-9e4d-e311-ba73-005056a60016" DisplayName="Canada">; <Updates>; <ivnt_FullName>; <First> - Canada</First>; <Second>DbNull</Second>; </ivnt_FullName>; </Updates>; </Location>; </Updates>;</Locations> <Locations Seq="7401" DbRevision="670">; <Updates>; <Location FRS_ID="B33153FEF4014CC6BAC40581564BC5E9" Frs_CompositeContract_Entity_Id="9d7073c2-9e4d-e311-ba73-005056a60016" DisplayName="Central">; <Updates>; <ivnt_FullName>; <First> - Central</First>; <Second>DbNull</Second>; </ivnt_FullName>; </Updates>; </Location>; </Updates>;</Locations>
Finally, with this second and final regex S/R :
SEARCH
;
REPLACE
\r\n
( or\n
for UNIX files )We get the expected OUTPUT text :
<Locations Seq="7400" DbRevision="670"> <Updates> <Location FRS_ID="30CD802CC0A744248253F1F869AADD8A" Frs_CompositeContract_Entity_Id="bdc85ad4-9e4d-e311-ba73-005056a60016" DisplayName="Canada"> <Updates> <ivnt_FullName> <First> - Canada</First> <Second>DbNull</Second> </ivnt_FullName> </Updates> </Location> </Updates> </Locations> <Locations Seq="7401" DbRevision="670"> <Updates> <Location FRS_ID="B33153FEF4014CC6BAC40581564BC5E9" Frs_CompositeContract_Entity_Id="9d7073c2-9e4d-e311-ba73-005056a60016" DisplayName="Central"> <Updates> <ivnt_FullName> <First> - Central</First> <Second>DbNull</Second> </ivnt_FullName> </Updates> </Location> </Updates> </Locations>
Best Regards,
guy038