Search code examples
bashawksolarisnawk

Matching fields across multiple files


I have two files: file1 and file2 as follows:

file1                    file2
orangejuice              orangejuice_9.88_9.88
pineapplejuice           appleslices_6.3_2.2
appleslices              pineapplejuice_1.2_3.9
Mangojuice               Mangojuice_5.55_5.55

The output should be:

orangejuice_988
pineapplejuice_120_390
appleslices_630_220
Mangojuice_555

While reading line by line from file1, search for the pattern found in the line of file1 in file2, once found compare the 2nd and 3rd fields of file2 , if they are the same print it once, if not print the two numbers.(The number should be multiplied by 100 anyways)

I thought of this:

while read -r -u 3 line1
do
nawk ' "$line1" print $0}' file2.txt
if "$2" == "$3"
then echo "scale=2;$2*100" |bc
else echo "$2_$3"
fi
done 3<file1.txt

So, I want to know if the logic is correct and fix the multiplication that would give me 988 instead of 988.0.


Solution

  • One way with GNU awk:

    $ awk 'NR==FNR{a[$0];next}($1 in a){print $1,$2==$3?$2*100:$2*100OFS$3*100}' FS=_ OFS=_ file1 file2
    orangejuice_988
    appleslices_630_220
    pineapplejuice_120_390
    Mangojuice_555