Hello
I hope you gyus will help me
I need to replace a number of entries in a document. Their are identical in structure.
Sample: stat_cost 1, 150, 350, 250, 250, 1150, 4, 340
I'll be always stat_cost same spacing then number, number, number, 250, 250, 1150, 4, 340
I figured out how to find all to entries: regular expressions and "stat_cost 1, ..., ..."
Now I need to know if I can make a formule to replace all like this
stat_cost 1, same number (different in every entry)multiplied by three, same number multiplied by three
so if first entry is
stat_cost 1, 100, 300, 350, 250, 250, 1150, 4, 340
i want it to be
stat_cost 1, 300, 900, 350, 250, 250, 1150, 4, 340
and if the second is
stat_cost 1, 400, 400, 350, 250, 250, 1150, 4, 340
i want it to be
stat_cost 1, 1200, 1200, 350, 250, 250, 1150, 4, 340
On first place there is always 1
I need to modify only second and third number, the rest dont matter
I guess it is simple but I am dumb. Hope you are smarter
Help will be appreciated wit virtual hug
Replacing random strings of numbers
-
- Posts: 6
- Joined: Sun Jun 23, 2019 1:41 am
Replacing random strings of numbers
OpenOffice 3.1 on Windows Vist
Re: Replacing random strings of numbers
Welcome to the Forums.
I see that you posted in the Writer portion of the forum, but what you appear to be wanting to do looks, to me, more like a Calc application.
Can you please confirm if you are wanting this for Writer or Calc?
I see that you posted in the Writer portion of the forum, but what you appear to be wanting to do looks, to me, more like a Calc application.
Can you please confirm if you are wanting this for Writer or Calc?
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
Re: Replacing random strings of numbers
(My answer assumes, it's Writer.)
Unfortunately I can only help you with a user routine. IIn Basic it reads:See attached demo.
You need to either permit macro execution for the document macro, or to move the macro to your local 'My Macros' standard library.
The demo was made with LibreOffice V 6.3.0.0.beta1, but should also work in (not too old?) AOO. It was tested with AOO V 4.1.5.
In Calc the situation would be similar. A solution based on standard functions would be very complicated if possible at all.
Unfortunately I can only help you with a user routine. IIn Basic it reads:
Code: Select all
Sub doIt()
REM Much to specialised, of course!
doc = ThisComponent
sd1 = doc.createSearchDescriptor()
sd1.SearchRegularExpression = True
sd1.SearchString = "(?<=idStr idNr)(, [0-9]+)+(?= )"
found = doc.findAll(sd1)
For j = 0 To found.Count - 1
jRg = found(j)
jSfound = jRg.String
jSrepl = ""
jSplit = Split(jSfound, ", ")
u = Ubound(jSplit)
For k = 1 To u
kNew = 3*jSplit(k)
jSrepl = jSrepl & ", " & kNew
Next k
jRg.String = jSrepl
Next j
End Sub
You need to either permit macro execution for the document macro, or to move the macro to your local 'My Macros' standard library.
The demo was made with LibreOffice V 6.3.0.0.beta1, but should also work in (not too old?) AOO. It was tested with AOO V 4.1.5.
In Calc the situation would be similar. A solution based on standard functions would be very complicated if possible at all.
- Attachments
-
- aoo98452verySpecialReplaceWithCalulatedResult_1.odt
- (13.25 KiB) Downloaded 177 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
---
Lupp from München
-
- Posts: 6
- Joined: Sun Jun 23, 2019 1:41 am
Re: Replacing random strings of numbers
Hey guys
Yes, unfortunately this is Writer, document is unreadable otherwise, as it is basic .txt
I also found that command "stat_cost 1, [digit:][digit:][digit:]" finds all those entries perfectly
I could settle on just multiplying just first digit of first [digit:] to get less variables but I fear that changes nothing as writer don't have command of "leave what you have found" or "alter it" in replace all section.
It is around 1400 entries in each of two files so doing it manually is rather tedious perspective
About those macros thank you immensely. Ido not know how it works but will try it.
Yes, unfortunately this is Writer, document is unreadable otherwise, as it is basic .txt
I also found that command "stat_cost 1, [digit:][digit:][digit:]" finds all those entries perfectly
I could settle on just multiplying just first digit of first [digit:] to get less variables but I fear that changes nothing as writer don't have command of "leave what you have found" or "alter it" in replace all section.
It is around 1400 entries in each of two files so doing it manually is rather tedious perspective
About those macros thank you immensely. Ido not know how it works but will try it.
OpenOffice 3.1 on Windows Vist
-
- Posts: 6
- Joined: Sun Jun 23, 2019 1:41 am
Re: Replacing random strings of numbers
I tried to run this macro it does nothing. No number changed. I simply run this and didn't do anything else. How it is supposed to do to make it work? Should I do something after running it? I have AOO 4.1.6Lupp wrote:(My answer assumes, it's Writer.)
Unfortunately I can only help you with a user routine. IIn Basic it reads:See attached demo.Code: Select all
Sub doIt() REM Much to specialised, of course! doc = ThisComponent sd1 = doc.createSearchDescriptor() sd1.SearchRegularExpression = True sd1.SearchString = "(?<=idStr idNr)(, [0-9]+)+(?= )" found = doc.findAll(sd1) For j = 0 To found.Count - 1 jRg = found(j) jSfound = jRg.String jSrepl = "" jSplit = Split(jSfound, ", ") u = Ubound(jSplit) For k = 1 To u kNew = 3*jSplit(k) jSrepl = jSrepl & ", " & kNew Next k jRg.String = jSrepl Next j End Sub
You need to either permit macro execution for the document macro, or to move the macro to your local 'My Macros' standard library.
The demo was made with LibreOffice V 6.3.0.0.beta1, but should also work in (not too old?) AOO. It was tested with AOO V 4.1.5.
In Calc the situation would be similar. A solution based on standard functions would be very complicated if possible at all.
OpenOffice 3.1 on Windows Vist
Re: Replacing random strings of numbers
Have you considered importing the data into Calc and manipulating it? Import each element into a separate cell, then do things [TM].
You might end up with something like
You could then manipulate the data as needed, and save it in a suitable format.
You might end up with something like
Code: Select all
A1 |B1 |C1 |D1 |E1 |F1 |G1 |H1 |I1 |J1
stat_cost |1 |1E100 |300 |350 |250 |250 |1150 |4 |340
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
Re: Replacing random strings of numbers
To be sure: Before I upload an example I test it. I just again took the time to download the attachment to my original answer again, and to run it under AOO 4.1.5 and under LibO 6.2.3.2 again. It worked as claimed and expected in both cases.dontcareiii wrote:I tried to run this macro it does nothing. No number changed. I simply run this and didn't do anything else. How it is supposed to do to make it work? Should I do something after running it? I have AOO 4.1.6
To be completely sure I also ran the openly posted Sub in a newly created document. Again: Worked correctly.
Do you know how to follow this advice? (I manwhile also tested with LibO 3.3 which is virtually the same as OOo 3.x. Worked ok!)Lupp wrote:You need to either permit macro execution for the document macro, or to move the macro to your local 'My Macros' standard library.
The demo was made with LibreOffice V 6.3.0.0.beta1, but should also work in (not too old?) AOO. It was tested with AOO V 4.1.5.
Lupp wrote:... Something idStr idNr, 12, 22, 32, 1042 something ...
"(?<=idStr idNr)(, [0-9]+)+(?= )"
Did you notice that my eaxmple pointed to generalisations using "idStr" in place of your "stat_cost" and " idNr" in place of your first " 1".dontcareiii wrote:
... stat_cost 1, 150, 350, 250, 250, 1150, 4, 340 ...
To run your own examples you would need to adapt the used RegEx to "(?<=stst_cost 1)(, [0-9]+)+(?= )" in place of "(?<=idStr idNr)(, [0-9]+)+(?= )". But, of course your next use case may require that you adapt the regular expression yourself - as you also will have to find the way of replacing your factor 3 from the first example to a probably later needed 2.73 .
Please attach a .odt file containg realistic examples of your style exactly.
-
- Posts: 6
- Joined: Sun Jun 23, 2019 1:41 am
Re: Replacing random strings of numbers
Lupp you're very helpful but I am too dumb for this. I didn't mean to accuse you, if you say you checked it, you did. I probably just don't know how to tell if this macro works.
Anyway I found a way, going around the problem, so I am not sure it is solved. My problem is solved but question from topic isn't. Or rather it is solved and what I wanted is impossible in Writer or Calc.
I will now write how I managed to approximate my failed multiplication.
I used basic replace all, it worked like a charm
I divided my targets in three groups
stat_cost 1, [:digit:][:digit:][:digit:][:digit:], for 4digit high numbers
stat_cost 1, [789][:digit:][:digit:], for 3digits of 900s, 800s, 700s,
stat_cost 1, [456][:digit:][:digit:], so on
stat_cost 1, [123][:digit:][:digit:],
and replaced with desire higher values
stat_cost 1, 3850, 580
stat_cost 1, 3550, 550
stat_cost 1, 1850, 350
stat_cost 1, 1350, 280
2400 in under 2 minutes
Remember to firstly deal with 4digit cause if you do 3 first you will move them up to original 4 tier
As I didn't need exact numbers but approximations to much higher values it worked well
Ingenuity from stupid people for stupid people
Anyway I found a way, going around the problem, so I am not sure it is solved. My problem is solved but question from topic isn't. Or rather it is solved and what I wanted is impossible in Writer or Calc.
I will now write how I managed to approximate my failed multiplication.
I used basic replace all, it worked like a charm
I divided my targets in three groups
stat_cost 1, [:digit:][:digit:][:digit:][:digit:], for 4digit high numbers
stat_cost 1, [789][:digit:][:digit:], for 3digits of 900s, 800s, 700s,
stat_cost 1, [456][:digit:][:digit:], so on
stat_cost 1, [123][:digit:][:digit:],
and replaced with desire higher values
stat_cost 1, 3850, 580
stat_cost 1, 3550, 550
stat_cost 1, 1850, 350
stat_cost 1, 1350, 280
2400 in under 2 minutes
Remember to firstly deal with 4digit cause if you do 3 first you will move them up to original 4 tier
As I didn't need exact numbers but approximations to much higher values it worked well
Ingenuity from stupid people for stupid people
OpenOffice 3.1 on Windows Vist