Sorting a CSV file by DOB


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting a CSV file by DOB
# 1  
Old 01-18-2013
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:

Code:
#!/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.
# 2  
Old 01-18-2013
Convert date to epoch and perform the sort. Here is an example:
Code:
#!/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:
Code:
$ 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

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-18-2013
The following seems a little bit simpler than bipinajith's script. Just add the following line to your script:
Code:
        sort -o $INPUT -n -t , -k4.7,4 -k4.4,4.5 -k4.1,4.2 $INPUT

after the following line in your script:
Code:
        INPUT=./birthday.csv

These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-18-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

2. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

3. Shell Programming and Scripting

Sorting the csv file in Perl

Hi All How all are doing today. Just struck in an issue in Perl I have a csv file which contain 32 column, I want to make sorting in that csv file with respect to 26th column. Is it possible to do so without any module being added? Regards Aditya ---------- Post updated at 10:02 AM... (11 Replies)
Discussion started by: adisky123
11 Replies

4. Shell Programming and Scripting

CSV Sorting on only particular columns

Hello! So ive been presented with this comma-delimited file: I need a print to look as below " lastname, phone_number, zip for every person with a last name starting with the letter H, I only with a 650-area code phone number. output should be sorted by reverse ZIP code " I only have... (5 Replies)
Discussion started by: strangemachine
5 Replies

5. Shell Programming and Scripting

Sorting a .csv using awk or other

Hello all, I am new here and *relatively* new to Unix. I have a bit of an emergency. I have a three column file that I need to sort: sample name, miRNA, reads per million (RPM) There are multiple samples, and for each sample name there are multiple miRNAs and associated RPMs. Some of these... (6 Replies)
Discussion started by: dunnybocter
6 Replies

6. Shell Programming and Scripting

Need help in writing a routine for sorting a CSV file

Hi, I have a CSV file in following manner: server1,env1,patch1 server1,env1,patch2 server1,env1,patch3 server1,env2,patch1 server1,env2,patch3 server2,env3,patch1 server2,env3,patch5 server2,env4,patch1 server3,env6,patch1 server3,env7,patch2 server3,env7,patch3 I want to... (6 Replies)
Discussion started by: avikaljain
6 Replies

7. UNIX and Linux Applications

UNIX sorting - csv file

Hi, Please help me to solve sorting in CSV file. I have 25 columns in my CSV. (Delimiter is ",") Summary columns are from 10 to 13 which are should not be sorted. From 1-9 and 14-25 should be able to sort in ascending. how should i do this using Sort command in unix. If no simple commands pls... (0 Replies)
Discussion started by: rajani_p
0 Replies

8. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies

9. Shell Programming and Scripting

Help sorting .csv file

Hi, I have a .csv file which contains script names, subjects and email_addresses The first two colums are always script name and subject, the next 20 colums are email address.. What i want to do is sort the email address in alphabetical order for each row and there's around 1200 rows. So... (3 Replies)
Discussion started by: Jazmania
3 Replies
Login or Register to Ask a Question