0

可能重复:
此脚本无法按年龄正确排序

我绝对不知道如何让这个脚本按出生日期对 Birthdays.csv 中的信息进行排序。我知道 sort -n 命令,但是我希望按 DOB 对文件birthdays.csv 进行排序。我该怎么做呢?

下面的脚本获取用户信息和出生日期,然后将这些信息放入一个名为“birthday.csv”的文件中。

然后我需要按出生日期对“birthdays.csv”进行排序,然后显示这个新排序的信息。我还计算了每个人到今天为止的年龄。我遇到的问题是按出生日期对birthdays.csv 中的信息进行排序。有人可以让我知道我会怎么做吗?

脚本如下:

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" >> Birthdays.csv
    echo If you would like to add another person press 1 or enter 2 to proceed
    read a
done

    INPUT=./Birthdays.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 "Name : $Name"
            echo "Surname : $Surname"
            echo "Telephone : $Telephone"
            echo "DOB : $DOB"
            echo "Age : $Age"
            echo "##########################################"
done < $INPUT
IFS=$OLDIFS
    echo $DATE

exit 0;
4

2 回答 2

1

添加这一行

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

INPUT=./Birthdays.csv
于 2013-01-18T18:22:43.270 回答
0

试试 -- ### cat Birthdays.csv | 排序 --field-separator='/' --key=3,5

于 2013-01-18T17:42:02.760 回答