dealing with dates in a text file

hi guys im trying to write a bash script that grabs expired domain names it leaves me with the following outpiut in a text file

Im hoping to try to normalise all the dates to the format displayed in the first 2 lines and remove the times so the file looks unfirom can any one give me a heads up ?

Thanks for looking

#!/bin/bash

while IFS=":" read THING TIME
do
        set -- $TIME

        case "$TIME" in
        [0-9][0-9]-???-[0-9][0-9][0-9][0-9])    ;;
        [0-9][0-9]-???-[0-9][0-9][0-9][0-9]" "*)
                TIME=$1
                ;;
        [jJ]an" "*|[fF]eb" "*|[mM]ar" "*|[aA]pr" "*|[mM]ay" "*|[jJ]un" "*|[jJ]ul" "*|[aA]ug" "*|[sS]ep" "*|[oO]ct" "*|[nN]ov" "*|[dD]ec" "*)
                TIME=$(printf "%02d-%s-%04d\n" $2 $1 $4)
                ;;
        *)
                echo "Unknown time format $TIME" >&2
                continue
                ;;
        esac

        echo "$THING:$TIME"
done < data

@corona688 much apprectiaed worked like a charm

thanks

You can use date itself to perform this,

$ for i in \
  "17-jun-2013" \
  "29-mar-2013" \
  "26-Jul-2012 04:00:00" \
  "Mar 26 23:59:59 2012" \
  "27-Jul-2012 07:56:43" ; \
do \
  date -d "$i" '+%d-%b-%Y' ; \
done ;
17-Jun-2013
29-Mar-2013
26-Jul-2012
26-Mar-2012
27-Jul-2012

When someone hasn't stated what their system is, odds are good an answer using GNU date will be useless; you don't find that mostly anywhere except Linux. Only fairly recent versions of GNU date have a -d that flexible, too.

When I find that date can't do things like this I often just build GNU Coreutils on the destination. The time it takes to compile is often far less than tweaking to get the date handling right myself :slight_smile:

So would I, on any machine I had administrator oversight on. But that's often a limitation too :frowning:

You shouldn't need administration, just a login really. Coreutils can compile happily in your home dir :slight_smile:

You need administrator privileges to install a compiler and headers. Quite a few production servers don't have them.

It might be possible to bodge those into your home directory as well, but we're moving well outside the realm of 'easiest alternative' here. :wink:

Sure, we don't have compilers on all the production machines we look after, but we have a build box which is of the same arch, so once compiled there it goes into my home dir which gets copied to all the production boxes. date is just one of those dead useful things (like gnufind/xargs/screen) which is helpful to have around.