2013-01-18 3 views
0

가능한 중복 :
This script wont sort correctly by agebash 스크립트에서 정렬하는 방법?

나는이 스크립트는 생년월일에 의해 Birthdays.csv에서 정보를 정렬하는 방법을 absolutaly 모른다. 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

[남자 정렬 (HTTP : //linux.die .net/man/1/sort) – peteches

+0

필자는 정렬 방법을 알고 있지만 파일의 각 사람의 나이별로 파일을 정렬하려고합니다. 그래서 정말로 알고 싶은 것은 파일 내의 누군가가 파일을 어떻게 정렬합니까? –

+0

@ChristianDiorHoward 당신은 거의'-k, --key = KEYDEF' (필드 분리 기호로'-t '사용)와 함께있을 것이지만, 날짜 형식에 대해 dd/mm/yyyy를 선택하면 사소한 것이됩니다 . –

답변

1

행을 추가

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

INPUT=./Birthdays.csv 
0

시도 - ### 고양이 Birthdays.csv | 정렬 --field 분리기 = '/'--key = 3,5-

관련 문제