2 Replies - 1365 Views - Last Post: 18 January 2013 - 12:45 PM Rate Topic: -----

#1 redshine6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 13

Sorting a CSV file by DOB

Posted 18 January 2013 - 10:38 AM

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;




Is This A Good Question/Topic? 0
  • +

Replies To: Sorting a CSV file by DOB

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: Sorting a CSV file by DOB

Posted 18 January 2013 - 11:25 AM

Well, from the looks of it, you are storing the date in dd/mm/yyyy format (though you are parsing it as if it was mm/dd/yyyy, so you may want to fix that).

If you are willing/ allowed to change the date format to be yyyymmdd (with no separators) it will make the process a lot quicker and easier. That will allow you to just use the sort command like so:
sort -t , -k 2n myInputFile.csv


(for more information on what this command specifically does see man sort).

Assuming the input file looks like so:
abc,123,asd
ads,100,asdf
agsfe,6,shrs
hsdfgs,76,dsgfzxv
rjdebr,1000,agdf
asdfawef,0,asfawe
awrh,9,efs0


And the second field is the birth date. At that point you should be able to pipe it to whatever you need such that you can determine age (or you could just sort after finding age and add an extra column to the input which will be sorted).

Hopefully that makes sense.
Was This Post Helpful? 1
  • +
  • -

#3 redshine6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 13

Re: Sorting a CSV file by DOB

Posted 18 January 2013 - 12:45 PM

Thank you for that, yeah I had noticed that error just after posting this, but thank you anyway.

I am needing to keep the formatting the same, so I have come to this solution instead:

add this line

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



After

INPUT=./Birthdays.csv


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1