Hello, I have a script where I monitor some notifications received via telegram, these notifications are redirected to a file called input.txt and the script is fed precisely by this file.
Example of the content inserted in input.txt:
#Price_Notification_ProductABC
Price: 0.006813
change: +1.6%
Count Number: 1
#Price_Notification_ProductABC
Price: 0.006969
change: +2.3%
Count Number: 2
#Price_Notification_ProductABC
Price: 0.007060
change: +1.3%
Count Number: 3
#Price_Notification_ProductABC
Price: 0.007300
change: +3.4%
Count Number: 4
#Price_Notification_ProductABC
Price: 0.007453
change: +2.1%
Count Number: 5
Output after script execution:
#Price_Notification_ProductABC
Price: 0.006813
change: +1.6%
Count Number: 1
#Price_Notification_ProductABC
Price: 0.006969
change: +3.9%
Count Number: 2
#Price_Notification_ProductABC
Price: 0.007060
change: +5.2%
Count Number: 3
#Price_Notification_ProductABC
Price: 0.007300
change: +8.6%
Count Number: 4
#Price_Notification_ProductABC
Price: 0.007453
change: +10.7%
Count Number: 5
The function of the script is to add incrementally the item “change:”
Up to this point everything is ok and the script has been working, however I have two problems and I need to implement two conditions for this to be solved.
1- The percentage needs to be calculated incrementally/increasingly, within a window of 0 to a maximum of 30 minutes between one notification and another, from minute 31, the incremental calculation of the percentage needs to be reset/restarted, starting a new one calculation cycle
2- I need a condition where the value contained in the item “Price:” in the current notification is not less than the value of the previous notification, example:
Current notification:
Price: 0.006825
Previous notification:
Price: 0.007453
If this occurs in any of the 2 scenarios, the cycle must be automatically restarted and the sum of percentages reset to zero.
Any ideas how I can solve this little problem in my script?
I have the 2 scripts below and they both do the same thing, if you can help me tweak the 2 scripts I’d appreciate it
Script in perl:
I execute as follows:
perl -lp ./perl.pl input.txt
#!/usr/bin/perl -lp
if (/^#Price_Notification_Product/) {
$prod = $_;
} elsif (/^change: ([^%]*)%/) {
$products{$prod} += $1;
$_ = "change: +$products{$prod}%";
Script with awk:
./awh.sh
#!/bin/bash
awk '
/^#Price_Notification_Product/ {
prod = $0;
}
/^change: / {
gsub(/[+%]/,"",$2);
products[prod] += $2;
$2 = "+" products[prod] "%"
}
1' /opt/scripts/input.txt