Find the answer to your Linux question:
Results 1 to 5 of 5
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    57

    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

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

    Code:
    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 Engineer
    Join Date
    Dec 2013
    Posts
    1,305
    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:
    Code:
    [1-3]{0,1}[0-9]\.1{0,1}[0-9]\.[1-2][0-9][0-9][0-9]
    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
    Just Joined!
    Join Date
    Apr 2012
    Posts
    57
    hi,

    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.

    Thanks

  4. #4
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,305
    You can read the file a line at a time.

    Code:
    while read line
    do
        for word in $line
        do
            if [[ $word =~ [1-3]{0,1}[0-9]\.1{0,1}[0-9]\.[1-2][0-9][0-9][0-9] ]]
            then
                # echo your change of date here
            else
                   echo -n "$word "
            fi
        done
        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.

  5. #5
    Just Joined!
    Join Date
    Feb 2007
    Posts
    22
    If you're only editing text, the "date" function might be irrelevant.

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


    Code:
    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:

    Code:
    sed 's/
    start the edit command

    Code:
    \([0-3]\?[0-9]\{1\}\)
    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)

    Code:
    \([0-1]\?[0-9]\{1\}\)
    look for and store the month number - like the day number but with value 00 - 19

    . look for a dot

    Code:
    20
    look for a "20" for the century number

    Code:
    \([0-9]\{2\}\)
    look for the last two digits of the year number and store this

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

    Code:
    \1\/\2\/20\3/g'
    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:
    Code:
    texte text 01.01.2012  chapter 32.1.14
    text text 05.03.2014  chapter 33.1.15
    becomes
    Code:
    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
  •