Sorting a CSV file by DOB

I have absolutaly no idea how to get this script to sort the info in Birthdays.csv by date of birth. I know the sort -n command, however i wish to sort the file birthdays.csv by DOB. How would i go about doing this?

The below script gets user info and date of birth and then puts these info a file called �birthday.csv�.
I then need to Sort �birthdays.csv� by date of birth and then display this newly sorted information. I also calculate how old each person is by today's date. The problem i have is sorting the info in birthdays.csv by date of birth. Can someone let me know how i would do this sort?

The script is below:

#!/bin/bash

a=0
while [ $a -lt 2 ];
do
        echo Please enter a first name
        read firstName
        echo Please enter last name
        read lastName
        echo Please enter phone number
        read phoneNumber
        echo Please enter date of birth - format dd/mm/yyyy
        read dob
        echo "$firstName,$lastName,$phoneNumber,$dob" >> birthday.csv
        echo If you would like to add another person press 1 or enter 2 to proceed
        read a
done

        INPUT=./birthday.csv
        OLDIFS=$IFS
        IFS=","
        [ -f ${INPUT} ] && while read Name Surname Telephone DOB
        do
                        birthMonth=${DOB:0:2}
                        birthDay=${DOB:3:2}
                        birthYear=${DOB:6:4}

                        currentDate=`date +%d/%m/%Y`

                        currentMonth=${currentDate:0:2}
                        currentDay=${currentDate:3:2}
                        currentYear=${currentDate:6:4}

                        if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$currentDay" -lt "$$birthDay" ]]
                        then
                                let Age=currentYear-birthYear-1
                        else
                                let Age=currentYear-birthYear
                        fi

                echo "---------------------------"
                echo "Name : $Name"
                echo "Surname : $Surname"
                echo "Telephone : $Telephone"
                echo "DOB : $DOB"
                echo "Age : $Age"
                echo "---------------------------"
done < $INPUT
IFS=$OLDIFS
        echo $DATE

exit 0;

Thank you in advance, all help is most appreciated.

Convert date to epoch and perform the sort. Here is an example:

#!/bin/bash

rm -f output.txt
while IFS=, read ft lt ph dt
do
        fdt=$( echo ${dt:3:2}/${dt:0:2}/${dt:6:4} )
        eph=$( date -d"$fdt" +"%s" )
        echo "${eph},${ft},${lt},${ph},${dt}" >> output.txt
done < birthday.csv

sort -t"," -n output.txt | awk -F, ' { print $2,$3,$4,$5; } ' OFS=,

Here is the output:

$ cat birthday.csv
First_1,Last_1,1234,08/10/1991
First_2,Last_2,1234,10/05/1990

$ ./sort_by_dt.sh
First_2,Last_2,1234,10/05/1990
First_1,Last_1,1234,08/10/1991
1 Like

The following seems a little bit simpler than bipinajith's script. Just add the following line to your script:

        sort -o $INPUT -n -t , -k4.7,4 -k4.4,4.5 -k4.1,4.2 $INPUT

after the following line in your script:

        INPUT=./birthday.csv
2 Likes

Thank you so much both of you. I am new to bash so bipinajith's solution was far too complicated for me to comprehend, haha. Perhaps one day, but not yet.

However Don Cragun, Thank you. I have been trying something similar, but could not get the parameters correct.

My script is now fully working, most appreciated.