For Each help needed to calculate

Hey Guys,

Need help to grab 2 valuta values and add the 2 amounts I think with For Each action.

Source:
some text amount 12.25EUR some text
some text
some text amount 12.25EUR some text
some text

regex created:

(?m)^(?:amount)(\d{1,4}\.\d{0,2})

Needed result:
€ 12.25 + € 12.25 = € 24.50
Any help appreciated,
Thx!

Regex seems like a way to do it, but I see a simpler way. Simply remove all characters from the stream that aren't digits or decimals. Then replace all newlines with a " " character. Then remove any duplicate spaces. Then replace each " " with "+". Then pipe the result through the macOS "bc" command. That should get the correct result. I'm doing this right now, but I'm having a little problem that I don't quite understand yet. I'm still working on this little bug.

Of course, if you don't like my way, you can do it another way. I enjoy challenges like this.

Okay, I put your sample text into a file, ran it through the following shell script, and it calculated the result correctly... 24.5

sed "s/[^[:digit:]\.]//g" | tr "\n" "+" | sed "s/++/+/g;s/+$//" | xargs | bc

I used some tricks to make that work. Some of my tricks may not be obvious, so you can ask me how this works if you want. This script may not work if you change the input a lot. I could make it work, but you'd have to show me an input that this script fails before I could fix it. It works with your example text.

HEHEHE... I just notice that 12.25+12.25 is 24.5, not 25. Do you have some new math that I don't know anything about? :smile:

Hey @Sleepy, thx for your quick reply. I will see if I can grasp your suggestion. As my basic math seems to lack, maybe I'm the sleepy one :wink:

Thx for now!

Hey @Sleepy, I was not complete in the sample data as I see now. Sorry about that. This is only a part which I posted. The grabbed portion will contain more values and amounts but not preceded with the text "amount". Only the two amounts preceded with the string amount should matched and added.
I need the actual result string to be like "€ 12.25 + € 12.25 = € 24.50" where amount 1 and amount 2 are placed in a variable and the sum is calculated and placed in one. Using the For Each will also give me an example I could use in future challenges.

Well, that changes a lot. I'm sorry that I misunderstood. Now I have to reconsider.

NO you did not misunderstood, I was not complete in my question, again sorry about that.

It sounds like you want each item in its own variable. In that case, the For Each doesn't seem to be relevant. The For Each action goes through multiple lines in the same variable, but that's not what you seem to want here. Can you clarify?

Hey Neo,

Please provide a real data sample, not just a description.

Descriptions are often hard to follow and frequently lack salient details.

-Chris

I thought I needed the For Each action to use as alternative for the /g (global) search and match. But yes I need the three values in 3 variables matched by the amount string for the first two amounts.

If I could I would.

I'm working on it, but I'm noticing that my regex expressions in KM aren't matching the same things in regex101.com, and I'm not sure why yet. ....... Ok, I think I've solved it. Hang on...

check RegEx: Global and Wiki

Try this solution... it worked for me. Or tell me what I misinterpreted.

Euros Macro (v10.0.2)

Euros.kmmacros (2.1 KB)

Does exactly what I was looking for, thanks alot!
What in the regex makes that the 2 amounts get in the 2 groups so you can place them in the 2 vars?

The first nine characters in my regex say "find the first series of digits or decimals."

The next seven characters say "skip past anything that isn't a digit."

The rest of the characters say "find the next series of digits or decimals."

Is that what you wanted to know? Or do you want me to explain how each group works?

got it! The possibilities of regex are seemingly endless

^Thanks again :slight_smile:

IHTYAH = I'm happy that you are happy.

The regex features in KM are pretty powerful, way more powerful than I can understand. However I still favor using shell commands for some of my character manipulation strategies. There are lots of thing in shell commands that go way beyond regex.

I should pickup some knowledge about shell commands too as addition on regex. Any good source you can recommend.

Even though there are lots of sources for learning UNIX/macOS, especially on YouTube, (hundreds of choices) it will always be true that the main source of knowledge for any command comes from the manual pages, which you can get using the man command, like this:

man tr
man bc
man sed

But another method, that even some admins don't know, is that you can get a list of relevant shell commands using the apropos command. For example, this command:

apropos URL

... will give you the name of all shell commands that have the string "URL" in their short description. I use the apropos command whenever I can't remember the name of a shell command, but when I can still remember a word that's probably in the command's description.

When you use apropos, you may have to flip through many pages of commands, because half the time the number of results from an apropos command numbers in the hundreds.

But for many people the easiest way to learn is to find a good YouTube video that's at your level and watch them show how the commands work. Here's how I would prioritize them for learning:

FIRST LEARN: ls, cd, pwd, mkdir, rmdir, mv, cp, cat, echo, more, less, history, rm, man, apropos
THEN LEARN: head, tail, find, sort, wc, touch, sort, uniq, diff, chmod, chown, grep, tr, touch, vi, nl, rev, say, seq
THEN LEARN: find, diff, xargs, awk, sed, egrep, ps, kill, df, du, bc, at, curl, cut, fmt, od, hexdump, md5, pbcopy, pbpaste, tee, whereis, which

That's how I would prioritize them. Everyone has their own priorities.

Thanks for your extended reply, I am familiar with man. Stays quite some commands and all possible switches per command.