#!/bin/bash

FILE_RANGE="./temp.txt"
FILE_COUNTRY_CODE="./GeoLite2-Country-CSV_20190806/GeoLite2-Country-Locations-fr.csv"
CODE=0

while read p; do
    CODE=$(awk -F ',' '{print $2}' <<< $p)
    RANGE=$(awk -F ',' '{print $1}' <<< $p)
    CONTINENT=$(awk -v country_code="$CODE" -F ',' '$0 ~ country_code {print $3}' $FILE_COUNTRY_CODE)
    case "$CONTINENT" in
        AS)
            printf "%s %s\n" "$RANGE" "$CONTINENT" >> AS.txt
            ;;
        OC)
            printf "%s %s\n" "$RANGE" "$CONTINENT" >> OC.txt
            ;;
        EU)
            printf "%s %s\n" "$RANGE" "$CONTINENT" >> EU.txt
            ;;
        AF)
            printf "%s %s\n" "$RANGE" "$CONTINENT" >> AF.txt
            ;;
        SA)
            printf "%s %s\n" "$RANGE" "$CONTINENT" >> SA.txt
            ;;
        NA)
            printf "%s %s\n" "$RANGE" "$CONTINENT" >> NA.txt
            ;;
    esac
done < temp.txt
