我绝对不知道如何让这个脚本按出生日期对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;
答案 0 :(得分:1)
添加此行
sort -o $INPUT -n -t , -k4.7,4 -k4.4,4.5 -k4.1,4.2 $INPUT
之后
INPUT=./Birthdays.csv
答案 1 :(得分:0)
尝试 - ### cat Birthdays.csv | sort --field-separator ='/' - key = 3,5