My input file contain list of username, and it may have name with number as a suffix (if duplicated).
Ex:
mary
john2
mike
john3
john5
mary10
alexa
So i want to check with a specific username (without suffix number) how many duplicated name, and what is the biggest number
Ex: with "john" -> ouput: 3 duplicates, biggest is 5
with "mary" -> ouput: 2 duplicates, biggest is 10
OK, base on the above perl script, I can translate it to bash script.
Here is the script:
name=$1
count=0;
last=0;
while read line
do
u=`echo $line |sed 's/[0-9]//g'`
n=`echo $line |sed 's/[a-zA-Z]//g'`
if [ "$u" = "$name" ]; then
count=`expr $count + 1`
if [ $n -gt $last ]; then
last=$n
fi
fi
done < user.txt
echo $count dups
echo $last is biggest
Now that you have something running, here's another.
#! /bin/ksh
#
if [ $# -eq 0 ] ; then
echo "Please enter a name"
exit 1;
fi;
name=$1
repeat=0
large=0
repeat=$(grep $name t)
set -- $repeat
repeat=$#
for list in "$@"
do
n=${list#$name}
if [[ -n $n ]] ; then
if [[ $n -gt $large ]] ; then
large=$n
fi;
fi;
done
echo "$name : Repeat=$repeat"
echo "$name : Largest=$large"
sub("[0-9]+$"," &") Add a space before the number in the input. For the input "mary10" the output is "mary 10". Now you have two fields one with name and other is number.
$2 > arr[$1] ? $2 : arr[$1] Here we are checking whether second field is greater than number already stored in array. If yes store that number to array.
For the first line 2 is second field and we are checking 2 > arr["john"]. Since this is the first time "john" is index and nothing is returned and two is greater and finally assigned to arr["john"].
For the second line 3 is second field and 3 > 2 is true and 3 is assigned to arr["john"]