Normalization Date Format

Source: Internet
Author: User
Tags month name

Normalization Date Format 01normdata. sh02 #! /Bin/sh03 04 # normdata. sh -- normalize the month to three letters on the date, and uppercase the first letter. 05 # useful functions for the 7th script valid-date. 06 07 monthnoToName () 08 {09 # Set the variable 'month' to an appropriate value of 10 case $1 in11 1) month = "Jan"; 2) month = "Feb"; 12 3) month = "Mar"; 4) month = "Apr"; 13 5) month = "May"; 6) month = "Jun"; 14 7) month = "Jul"; 8) month = "Aug"; 15 9) month = "Sep"; 10) month = "Oct"; 16 11) month = "Nov"; 12) month = "Dec"; 17 *) echo "$0: unknown numeric month value $1 "> & 2; exit 118 esac19 return 020} 21 22 ### main part of the script 23 24 if [$ # -Ne 3]; then25 echo "Usage: $0 month day year "> & 226 echo" Typical input formats are August 3 1962 and 8 3 2002 "> & 227 exit 128 fi29 30 if [$3-lt 99] | [$3-> 9999]; then31 echo "$0: expected four-digit year value. "> & 2; exit 132 fi33 34 if [-z $ (echo $1 | sed's/[[: digit:] // G')]; then35 monthnoToName $136 else37 # Three letters starting with normalization, the first capital, and the remaining lowercase 38 month = "$ (echo $1 | cut-c1 | tr '[: lower:] ''[: Upper:] ') "39 month =" $ month $ (echo $1 | cut-c2-3 | tr' [: upper:] ''[: lower:]') "40 fi41 42 echo $ month $2 $343 44 exit 0 script how to work Note 3rd condition statements: if [-z $ (echo $1 | sed's /[[: digit:] // G')]; then removes all numbers and uses-z to test whether the result is null. If the result is blank, the first input field must be one or more numbers. Therefore, the monthnoToName function is called to map it to the name of a month. If it is not empty, use two subshell escape sequences (the sequence enclosed by the $ symbol and left and right parentheses) to call the encapsulated command, then replace the output). A pipeline consisting of cut and tr is used to generate a month. The first sequence only extracts the first character, and then uses the tr command to capitalized it. (Note: The sequence echo $1 | cut-c1 can also be written as $ {1% ${1 #?}}.) The second sequence extracts 2nd and 3rd characters and then lowercase them. Run the Script: To ensure maximum adaptability to scripts containing the normdata function in the future, the script is designed to accept input from three fields on the command line. If you only want to use this script interactively, in contrast, you should prompt that the user has three domains. However, it is not convenient to call normdate from other scripts. Result: This script completes what we expected: as long as the date format is relatively simple, it can be normalized (known month name, month value between 1-12, there is also a four-digit year ). Example: result 1. /normdate. sh 8 3 62 2. /normdate. sh: expected four-digit year value. 3. /normdate. sh 8 3 1962 4 Aug 3 1962 5. /normdate. sh AUGust 3 1962 6 Aug 3 1962 extended reading: before you get too excited because you can add a lot of extensions to this script, check out script 7, it uses normdate to enter the correct date. In any case, one of the changes you can make now is to immediately add the following code to the testing part at the beginning of the script, your script can accept a date in the format of MM/DD/YYYY or MM-DD-YYYY: 1 if [$ #-eq 1]; then 2 set -- $ (echo $1 | sed's/[\/\-] // G') 3 fi test: 1. /normdate. sh March-11-1911 2 Mar 11 1911 3. /normdate. sh 8/3/1962 4 Aug 3 1962 the final script is: 01 complete normdate. sh02 #! /Bin/sh03 04 monthToName () 05 {06 case $1 in07 1) month = "Jan"; 2) month = "Feb"; 08 3) month = "Mar"; 4) month = "Apr"; 09 5) month = "May"; 6) month = "Jun"; 10 7) month = "Jul"; 8) month = "Aug"; 11 9) month = "Sep"; 10) month = "Oct"; 12 11) month = "Nov"; 12) month = "Dec"; 13 *) echo "$0: Unknown numeric month value $1"> & 2; exit 114 esac15 return 016} 17 18 if [$ #-eq 1]; then19 set -- $ (echo $1 | sed's/[\/\-] // G') 20 fi21 22 if [$ #-ne 3]; then23 echo "Usage: $0 month day year "> & 224 echo" Typical input formats are August 3 1962 and 8 3 2002 "> & 225 exit 126 fi27 28 if [$3-lt 99] | [$3-> 9999]; then29 echo "$0: expected four-digit year value. "> & 2; exit 130 fi31 32 if [-z $ (echo $1 | sed's/[[: digit:] // G')]; then33 monthToName $134 else35 month = "$ (echo $1 | cut-c1 | tr '[: lower:]' [: upper:] ') "36 month =" $ month $ (echo $1 | cut-c2-3 | tr' [: upper:] ''[: lower:] ') "37 fi38 39 echo $ month $2 $340 41 exit 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.