#!/bin/bash # Check if two arguments (input and output files) are provided if [ "$#" -ne 2 ]; then echo "Usage: $0 input_file.csv output_file.csv" exit 1 fi input_file="$1" # First script parameter: input CSV file output_file="$2" # Second script parameter: output CSV file rm -f "$output_file" # Initialize an associative array to store the pairs and their corresponding last two numbers declare -A pair_map # Read the input file line by line while IFS=, read -r first second third fourth fifth sixth seventh eighth; do # Create a unique key for the pair (first,second) key="$first,$second" reverse_key="$second,$first" # If the reverse key already exists in the array, compare the last two numbers if [[ -n "${pair_map[$reverse_key]}" ]]; then # Get the values stored for the reverse pair reverse_line="${pair_map[$reverse_key]}" reverse_seventh=$(echo "$reverse_line" | cut -d',' -f7) reverse_eighth=$(echo "$reverse_line" | cut -d',' -f8) # Compare the last two numbers and keep the line with the larger of the two if (( $(echo "$seventh > $reverse_seventh" | awk '{print ($1 > $2)}') )) && \ (( $(echo "$eighth > $reverse_eighth" | awk '{print ($1 > $2)}') )); then echo "$first,$second,$third,$fourth,$fifth,$sixth,$seventh,$eighth" >> "$output_file" else echo "$reverse_line" >> "$output_file" fi # Remove the reverse_key from the array, as it has already been handled unset pair_map["$reverse_key"] else # Store the current line in the array pair_map["$key"]="$first,$second,$third,$fourth,$fifth,$sixth,$seventh,$eighth" fi done < "$input_file" # Optional: Output a message indicating the completion of the script echo "Processing completed. Check $output_file for results."