Difference script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference script
# 1  
Old 05-04-2010
Difference script

Hi All,

I will need a script which takes an difernce of the 3rd column of report generated yesterday with the report generated today and has the differnce as a last colums in a new result file.

Code:
 
yday.txt:
UNDOTBS1                                  15.00            10.00        5.00 
UNDOTBS2                                  20.00            15.00        5.00 
USERS                                      5.00             3.00        2.00 

today.txt:
UNDOTBS1                                  15.00            12.00        3.00 
UNDOTBS2                                  20.00            17.00        3.00 
USERS                                      5.00             4.00        1.00 
 
required.txt: (today.txt + column with differnce)
UNDOTBS1                                  15.00            12.00        3.00    2.00
UNDOTBS2                                  20.00            17.00        3.00    2.00
USERS                                      5.00             4.00        1.00    1.00

# 2  
Old 05-04-2010
what have you tried till now??
# 3  
Old 05-04-2010
Try and adapt the following AWK command :
Code:
awk '
function abs(n) {
   return (n<0 ? -n : n)
}
NR==FNR { Old[$1] = $3 ; next }
{
   diff = ""
   if ($1 in Old && $3 != Old[$1] )
      diff = sprintf("   %5.2f", abs($3-Old[$1]));
   print $0 diff
}
' yday.txt today.txt

Jean-Pierre.
# 4  
Old 05-05-2010
Hi ,

Thanks for the script , this works perfectly fine, when the digits are 10.00, 15.00 and so on..,
If the number is like 11,229.06, 98,532.02 please let me know what modification the script will need?

Thanks a lot.

Last edited by jjoy; 05-05-2010 at 04:18 AM..
# 5  
Old 05-05-2010
Try this new version (the function toStr is a little bit ugly) :
Code:
awk '

function abs(n) {
   return (n<0 ? -n : n)
}

function toNum(s) {
   sub(/,/, "", s);
   return s+0;
}

function toStr(n, w, p        , str, sign, len, dec, num) {
   str = sprintf("%." p "f", n+0);
   if (str ~ /^[+-]/) {
      sign = substr(str, 1, 1);
      str  = substr(str,2);
   }
   dec = str;
   sub(/.*\./, "", dec);
   sub(/\..*/, "", str);
   num = "";
   for (len=length(str); len>3; len-=3) {
      num = "," substr(str, len-2, 3) num
   }
   num = substr(str, 1, len) num;
   sub(/^,/, "", num);
   return  sprintf("%" w "s", sign num "." dec);
}

NR==FNR { Old[$1] = toNum($3) ; next }
{
   diff = ""
   new  = toNum($3);
   if ($1 in Old && new != Old[$1] )
      diff = toStr(abs(new-Old[$1]), 15, 2);
   print $0 diff
}
' yday.txt today.txt

yday.txt
Code:
UNDOTBS1                                  15.00            10.00        5.00 
UNDOTBS2                                  20.00            15.00        5.00 
FOO                                        1.00             1.00        1.00
USERS                                      5.00             3.00        2.00 
BONUS                                    123.45         1,111.11      200.00

today.txt
Code:
UNDOTBS1                                  15.00            12.00        3.00
UNDOTBS2                                  20.00            17.00        3.00
FOO                                        1.00             1.00        1.00
USERS                                      5.00             4.00        1.00
BONUS                                    777.00         3,333.33       33.00

Output:
Code:
UNDOTBS1                                  15.00            12.00        3.00           2.00
UNDOTBS2                                  20.00            17.00        3.00           2.00
FOO                                        1.00             1.00        1.00
USERS                                      5.00             4.00        1.00           1.00
BONUS                                    777.00         3,333.33       33.00       2,222.22

Jean-Pierre.
# 6  
Old 05-06-2010
Hi,

Thanks a lot, this script works real great.
I have just one more addition to this, hopefully i am not asking too much.
Is there any way, that the if the difference can be made 0. Shown it as bold in the code below.
Code:
Output:

Code:
UNDOTBS1                                  15.00            12.00        3.00           2.00
UNDOTBS2                                  20.00            17.00        3.00           2.00
FOO                                        1.00             1.00        1.00           0.00
BONUS                                    777.00         3,333.33       33.00       2,222.22


Last edited by jjoy; 05-06-2010 at 07:46 AM..
# 7  
Old 05-06-2010
Another new version of the script :
Code:
awk '

function abs(n) {
   return (n<0 ? -n : n)
}

function toNum(s) {
   sub(/,/, "", s);
   return s+0;
}

function toStr(n, w, p        , str, sign, len, dec, num) {
   str = sprintf("%." p "f", n+0);
   if (str ~ /^[+-]/) {
      sign = substr(str, 1, 1);
      str  = substr(str,2);
   }
   dec = str;
   sub(/.*\./, "", dec);
   sub(/\..*/, "", str);
   num = "";
   for (len=length(str); len>3; len-=3) {
      num = "," substr(str, len-2, 3) num
   }
   num = substr(str, 1, len) num;
   sub(/^,/, "", num);
   return  sprintf("%" w "s", sign num "." dec);
}

NR==FNR { Old[$1] = toNum($3) ; next }
{
   diff = 0
   new  = toNum($3);
   if ($1 in Old && new != Old[$1] )
      diff = abs(new-Old[$1]);
   diff = toStr(abs(diff), 15, 2);
   print $0,diff
}
' yday.txt today.txt

Output:
Code:
UNDOTBS1                                  15.00            12.00        3.00            2.00
UNDOTBS2                                  20.00            17.00        3.00            2.00
FOO                                        1.00             1.00        1.00            0.00
USERS                                      5.00             4.00        1.00            1.00
BONUS                                    777.00         3,333.33       33.00        2,222.22

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Difference in executing the script

Hi Team, a silly question. Let's say i have a script called xyz.ksh what is the difference in executing the script as follows? ./xyz.ksh ksh xyz.ksh Thanks (2 Replies)
Discussion started by: kmanivan82
2 Replies

3. Shell Programming and Scripting

Weird difference in script execution

Hi, So I have a very simple script which loops over 5 times and prints the iterator value. #!/bin/sh START=1 END=5 for i in $(eval echo "{$START..$END}") do echo "$i" done If I save this script in a .sh file and run it in the terminal, the output I get is {1..5} (4 Replies)
Discussion started by: jamie_123
4 Replies

4. Shell Programming and Scripting

What is difference between this two lines in script?

Hi Guys, What is difference between this two lines in script logger -p daemon.info -t postback Starting /opt/local/bin/backup-report and /opt/local/bin/backup-report is the backu script running twice here? Thanks, (2 Replies)
Discussion started by: manalisharmabe
2 Replies

5. Programming

what is the main difference between difference between using nonatomic lseek and O_APPEND

I think both write at the end of the file ...... but is there a sharp difference between those 2 instruction ..... thank you this is my 3rd question today forgive me :D (1 Reply)
Discussion started by: fwrlfo
1 Replies

6. Shell Programming and Scripting

Need script to output difference in folders

Hey everyone, I have two folders (folder1, folder2). Folder2 is a compiled version of a bunch of other folders including folder1. I want to compare folder1 to folder2 to make sure that folder2 contains all of the contents of folder1. If it does not, I would like the script to output... (5 Replies)
Discussion started by: chango77747
5 Replies

7. Shell Programming and Scripting

AWK Script and Commandline difference

Hey there, I just stumbled upon a difference between using awk on the commandline and using it in a shellscript. I have a variable, e.g.: PROG=vim then i want to check if the package with this name is installed: TEMPVAL=$(dpkg -l | awk '{ if ($2 == "$PROG") print $2 }') (Im using... (10 Replies)
Discussion started by: MrSnail
10 Replies

8. Shell Programming and Scripting

Script to display time difference..

Hi i've written a script which reads last two line of the log file from N number of servers and send the mail by redirecting to a particular log file. And the two lines is displayed below. Oracle Q03 Begin Hot BACKUP Time: 07/23/08 18:35:46 Oracle Q03 End Hot BACKUP Time: 07/24/08 14:18:15... (1 Reply)
Discussion started by: suri.tyson
1 Replies

9. Shell Programming and Scripting

Difference in Executing a Script

Can anyone tell me the difference between the following execution ways: 1) . ./filename 2) ./filename Also when to use the either. (8 Replies)
Discussion started by: Shivdatta
8 Replies
Login or Register to Ask a Question