Skip to content
Advertisement

comparing two files with different columns

i have the two files(count.txt, count1.txt). i need to do the following
1. get the values from count.txt and count1.txt where 1st column is equal.
2. if its equal need to compare the 2nd column like ((1st column value + 5) >= 2 column value)

count.txt

order1,150
order2,165
order3,125

count1.txt

order1,155
order2,170
order3,125
order4,123

and i want the output like below,

Output.txt

order1,155
order2,170

i have used below nawk command for the 1st point, but not able to complete the 2nd point. Please suggest to achieve the same.
nawk -F"," 'NR==FNR {a[$1];next} ($1 in a)' count.txt count1.txt

Advertisement

Answer

nawk -F"," 'NR==FNR {a[$1]=$2;next} ($1 in a) && (a[$1]+5)<=$2' count.txt count1.txt
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement