Sponsored Content
Top Forums Shell Programming and Scripting String Comparison between two files using awk Post 302081442 by Glenn Arndt on Tuesday 25th of July 2006 10:33:37 AM
Old 07-25-2006
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!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparison of two files in awk

Hi, I have two files file1 and file2 delimited by semicolon, And I want to compare column 2 and column3 of file1 to column3 and column 4 in file2. file1 -------- abc;cef;155.67;143_34; def;fgh;146.55;123.3; frg;hff;134.67;; yyy;fgh;134.78;35_45; file 2 --------- abc;cef;155.09;;... (12 Replies)
Discussion started by: jerome Sukumar
12 Replies

2. Shell Programming and Scripting

Awk Comparison of 2 specific files

Hi Everybody, I know the topic sounds familiar but I just couldn't adapt or find the right code that solves my particular issue. I really hope you can help. I would like to compare 2 files in an awk script. Both files have different paths. The awk script call should look like that awk -f... (7 Replies)
Discussion started by: hhoosscchhii
7 Replies

3. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

4. UNIX for Dummies Questions & Answers

df -> output files; comparison using awk or...

:wall: I am trying to do the following using awk (is that the best way?): Read 2 files created from the output of df (say, on different days) and compare the entries using the 1st (FileSys) and 6th (Mount) fields to see if the size has changed. Output (at least), to a new file (some header... (2 Replies)
Discussion started by: renata
2 Replies

5. Shell Programming and Scripting

comparison of 2 files using unix or awk

Hello, I have 2 files and I want them to be compared in a specific fashion file1: A_1200_1250 A_1251_1300 B_1301_1350 B_1351_1400 B_1401_1450 C_1451_1500 and so on... file2: 1210 1305 1260 1295 1400 1500 1450 1495 Now The script should look for "1200" from A_1200_1250 of... (8 Replies)
Discussion started by: Diya123
8 Replies

6. Shell Programming and Scripting

AWK string comparison

Hey guys.. New in linux scripting and need some help on some scripting with history command. I managed to export the command history into a file and now i'm trying to select from that file some specific commands that were made in a certain period. Here's what i got so far echo -n... (2 Replies)
Discussion started by: mishu_cgm
2 Replies

7. Shell Programming and Scripting

Awk: Replacement using 2 diff files input and comparison

Requirement: If $5(date field) in ipfile is less than $7(date field) in deact file & $1 of ipfile is present in deactfile then $1 to be replaced by $2,$3,$4,$5,$6 of deact file else if $5(date field) in ipfile is greater than $7(date field) in actfile & $1 of ipfile is present in actfile then... (5 Replies)
Discussion started by: siramitsharma
5 Replies

8. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

9. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

10. UNIX for Beginners Questions & Answers

awk comparison using multiple files

Hi, I have 2 files, I need to use column of file1 and do a comparison on file2 column 1 and print the mismatch is file3 as mentioned below. Kindly consider that file 1 is having uniq key(column) whereas in file2 we have multiple duplicates (like 44). These duplicates should not come in... (2 Replies)
Discussion started by: grv
2 Replies
All times are GMT -4. The time now is 07:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy