Ubuntu – AWK–Comparing the value of two variables in two different files

bashtext processing

I have two text files A.txt and B.txt. Each line of A.txt A.txt

100
222
398

B.txt

1  2  103  2 
4  5  1026 74
7  8  209  55
10 11 122  78

What I am looking for is an awk command for doing this:

for each line of A 
    search B;
    if (the value of third column in a line of B - the value of the variable in A > 10)
        print that line of B;

Best Answer

The following script should solve your problem:

#!/bin/bash
A="$HOME/a.txt"
B="$HOME/b.txt"

cat $A | while read a; do
    cat $B | while read b; do
        b3=$(echo $b | awk ' { print $3 }')
        c=$(($b3 - $a))
        if (( $c > 10 )); then
            echo $b
        fi
    done
done

Don't forget to make it executable using the following command:

chmod +x script_name