The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[sh] String comparison operators userix Shell Programming and Scripting 1 05-16-2008 04:09 AM
string comparison Jatsui Shell Programming and Scripting 5 02-04-2008 04:28 PM
string comparison fedora Shell Programming and Scripting 2 01-03-2007 03:20 PM
Help with if loop (string comparison) psynaps3 Shell Programming and Scripting 4 07-07-2006 02:36 AM
String Comparison abey High Level Programming 1 10-19-2005 12:08 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-24-2006
rudoraj rudoraj is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 41
String Comparison between two files using awk

I have two files with field seperator as "~".
File A: 12~13~14~15
File B: 22~22~32~11

i want to calculate the difference between two files and than calculate the percentage difference and output it to a new file.
How do i do this using awk.

Also please suggest GOOD awk tutorials.

Thank you
  #2 (permalink)  
Old 07-24-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Of which fields do you want to calculate the difference? Which fields are you comparing between the two files?
  #3 (permalink)  
Old 07-24-2006
rudoraj rudoraj is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 41
I want to get difference of each column from each file
example
File A: column 1 - File B: column 1
File A: column 2 - File B: column 2
and so on
  #4 (permalink)  
Old 07-24-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Quick and dirty, and I made a lot of assumptions. This outputs the difference and percentage difference between corresponding fields in fileA.txt and fileB.txt. Output format for each pair of fields is difference/percentage --

cat fileA.txt:
Code:
12~13~14~15
16~83~10~44
75~84~96~56
cat fileB.txt:
Code:
22~22~32~11
73~85~52~43
74~51~14~68
Code:
paste file[AB].txt | sed 's/	/~/g' | \
awk '
  BEGIN { FS="~" }
  {
    divs=NF/2
    for ( i = 1; i <= divs; i++ ) {
      f1=i
      f2=i+divs
      diff=$f1-$f2
      pct=diff/$f1
      printf("%.0f/%.2f ",diff,pct)
    }
    printf("\n","")
  }'
Yields:
Code:
-10/-0.83 -9/-0.69 -18/-1.29 4/0.27
-57/-3.56 -2/-0.02 -42/-4.20 1/0.02
1/0.01 33/0.39 82/0.85 -12/-0.21
  #5 (permalink)  
Old 07-24-2006
rudoraj rudoraj is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 41
Great, this is what i wanted to see as output.....
  #6 (permalink)  
Old 07-25-2006
patras patras is offline
Banned
  
 

Join Date: May 2006
Posts: 25
Quote:
paste file[AB].txt | sed 's/ /~/g' | \
awk '
BEGIN { FS="~" }
{
divs=NF/2
for ( i = 1; i <= divs; i++ ) {
f1=i
f2=i+divs
diff=$f1-$f2
pct=diff/$f1
printf("%.0f/%.2f ",diff,pct)
}
printf("\n","")
}'
Glenn,

Can you pls explain the above code to me, I tried to understand it, but couldn't figure it out, your reply would be highly appreciated. Thanks.

Cheers,
Patras
  #7 (permalink)  
Old 07-25-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Sure thing...

fileA.txt contains:
Code:
12~13~14~15
16~83~10~44
75~84~96~56
fileB.txt contains:
Code:
22~22~32~11
73~85~52~43
74~51~14~68
The code:
Code:
paste file[AB].txt | sed 's/	/~/g' | \
awk '
  BEGIN { FS="~" }
  {
    divs=NF/2
    for ( i = 1; i <= divs; i++ ) {
      f1=i
      f2=i+divs
      diff=$f1-$f2
      pct=diff/$f1
      printf("%.0f/%.2f ",diff,pct)
    }
    printf("\n","")
}'
Paste files A and B together, records side by side (the whitespace between them is a tab):
Code:
paste file[AB].txt
Code:
12~13~14~15     22~22~32~11
16~83~10~44     73~85~52~43
75~84~96~56     74~51~14~68
Replace tabs with tildes (~), so we have one record pair per line:
Code:
sed 's/	/~/g'
Code:
12~13~14~15~22~22~32~11
16~83~10~44~73~85~52~43
75~84~96~56~74~51~14~68
...which we pipe through awk:
Set the field separator to ~
Code:
awk '
  BEGIN { FS="~" }
We're assuming that both files contain the same number of records, and that each
record pair in the files contains an equal number of fields. Therefore, we want
to split the whole record, e.g. "12~13~14~15~22~22~32~11" into two parts. So we
set divs equal to half the number of fields:
Code:
  {
    divs=NF/2
In this case, each file has four fields, so we are going to find the differences
of fields 1 and 5, 2 and 6, 3 and 7, and 4 and 8. The "divs=NF/2" method just
lets us determine at run time how many fields we are dealing with:
Code:
Field #:  1  2  3  4  5  6  7  8
Record:  12~13~14~15~22~22~32~11
For each joined record pair, we want to perform as many calculations as there are
record pairs. The number of record pairs is determined dynamically for each line:
Code:
    for ( i = 1; i <= divs; i++ ) {
Set f1 equal to our iterator. This will be the fileA.txt value for that field number.
It gets incremented after each comparison:
Code:
      f1=i
Set f2 equal to the iterator plus the number of fields in each div, e.g. if i is field 1,
then we want f2 to be field 5. So f2=i+divs --> f2=1+4.
Code:
      f2=i+divs
Calculate the difference between field1 and field2. This is tricky because we are not
subtracting the value of f2 from the value of f1; f1 and f2 are pointers (i.e. field numbers).
So $f1 is $1 and $f2 is $5, then on the next iteration $f1 is $2 and $f2 is $6, and so on:
Code:
      diff=$f1-$f2
Same concept -- just take the difference and divide by the fileA value:
Code:
      pct=diff/$f1
Print output of diff and pct, using formats %.0f and %.2f respectively (this is arbitrary; I
just chose those formats for looks).
Code:
      printf("%.0f/%.2f ",diff,pct)
After the loop is finished for the record pair, print a new line:
Code:
    }
    printf("\n","")
Code:
}'
All done!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0