Here's a solution. I have made a number of assumptions.
1. You are using a newer version of Bash that accepts associative arrays. You can check by issuing command: declare -A arr
2. Actual data starts with line number 3, so I processed the file starting line 3.
Code:
#!/bin/bash
FILE=$1
#Find all the duplicates
dups=$(cat $FILE | awk '{print $1}'| sort | uniq -d)
#Keep track of duplicate values
declare -A count
for val in $dups
do
count[$val]=1
done
#Now lets process the file one line at a time
sed -n '3,$ p' $FILE | while read line
do
#Get the first field
f1=$(echo "$line" | awk '{print $1}')
if [[ -n ${count[$f1]} ]]
then
#value is duplicate
echo "$line" | sed "s/\($f1\)/\1_NotUniq_${count[$f1]}/"
(( count[$f1]++ ))
else
echo $line | sed "s/\($f1\)/\1_Unique/"
fi
done
USAGE:
Code:
./script_name filename
NOTE: I have tested the code before posting.
From: Farzan Jameel Mufti Thursday July 12, 2012