Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Check date's format and change its format

    Hi all,

    I have a text that contain dates and maybe number of chapters that look like a date and I would like to convert the dates in another format if they are valid.

    For example, the text

    Today is 2.6.2014 and I read the chapter 32.1.14
    should be converted into

    Today is 2/6/2014 and I read the chapter 32.1.14
    I know to use the date -d "$date" but I donít know how to keep the date in a var and then make the replacement.

    Do you have any idea?

    Thanks for your time.

  2. #2
    Linux Guru
    Join Date
    Dec 2013
    Victoria, B.C. Canada
    A good example since it clearly shows the challenge. To make it work you'll need to decide what limitations on the dates and chapters can be supposed.

    For instance - will dates always have 4 digits and chapters never have 4 digit subsections in the 3rd position? If so then a regex such as:
    Assuming I got it right, this would limit the match a limited range that dates fall into but only 0-39 in the first position, 0-12 in the second and 1000-2999 in the third. This matches some dates that are invalid. If some of the chapters will also match it then it won't help one way or the other. In the example you gave it should match the date but not the chapter.

  3. #3

    thank you for your reply. I have done something in perl that recognise the dates and change them but it is based on a regex. It would be great if I could use the date command so the dates like 30/2 will recognised as invalid.

    Actually, I am looking on how I can save the date a a var, and the rest I guess will be easy. Then the other issue is that I cannot get verbose result for date.


  4. $spacer_open
  5. #4
    Linux Guru
    Join Date
    Dec 2013
    Victoria, B.C. Canada
    You can read the file a line at a time.

    while read line
        for word in $line
            if [[ $word =~ [1-3]{0,1}[0-9]\.1{0,1}[0-9]\.[1-2][0-9][0-9][0-9] ]]
                # echo your change of date here
                   echo -n "$word "
        echo -e "\n"
    done <file
    Something along the lines of the above will make the change to standard out. You can redirect it to a file.

  6. #5
    If you're only editing text, the "date" function might be irrelevant.

    One way to do what I think you want done is:

    sed 's/\([0-3]\?[0-9]\{1\}\).\([0-1]\?[0-9]\{1\}\).20\([0-9]\{2\}\)/\1\/\2\/20\3/g'
    Which is to edit the text you already have (sed) in the following way:

    sed 's/
    start the edit command

    look for the day of the month number - a number between 1 and 31 and store this number. Do this by looking for zero or one occurrences of the number 0 to 3 (in case this number is ever zero padded, otherwise 1 to 3 will work) followed by one occurrence of any digit 0-9. The () brackets store this number (value 00-39)

    . look for a dot (period, whatever you call this)

    look for and store the month number - like the day number but with value 00 - 19

    . look for a dot

    look for a "20" for the century number

    look for the last two digits of the year number and store this

    [CODE]/[CODE] start the " what to put in its place" bit

    the "\1", "\2", "\3" bits say to put the numbers stored in the previous bit here and the escaped forward slashes are the day / month / year separator bit that gets printed.

    In my trial, the following text:
    texte text 01.01.2012  chapter 32.1.14
    text text 05.03.2014  chapter 33.1.15
    texte text 01/01/2012  chapter 32.1.14
    text text 05/03/2014  chapter 33.1.15
    As long as the year in your date is in the ccyy (2014) rather than yy (14) format and the chapters don't go up to 21.1.20xx this should work

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts