You are on page 1of 1

#!

/bin/bash

if [[ $# -ne 1 ]]; then


echo "Usage: $0 <input_file>"
exit 1
fi

input_file="$1"

# Read the file and store lines in an array


mapfile -t lines_array < "$input_file"

declare -A occurrence_map

# Loop through each line in the array


for line in "${lines_array[@]}"; do
# Extract the last part of the line
last_part=$(echo "$line" | awk '{print $NF}')

# Count the occurrences in the associative array


((occurrence_map["$last_part"]++))
done

# Print the unique occurrences


for key in "${!occurrence_map[@]}"; do
echo "$key: ${occurrence_map[$key]}"
done
Save this script into a file, for example, count_unique_last_part.sh, and make it
executable:

bash
Copy code
chmod +x count_unique_last_part.sh
Then, you can run the script by passing the path to the text file as an argument:

bash
Copy code
./count_unique_last_part.sh input.txt
Replace input.txt with the path to your input text file. The script will read the
file, store lines in the lines_array, count the unique occurrences of the last part
of each line, and display the results on the console.

You might also like