LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   dealing with dates in a text file (https://www.linuxquestions.org/questions/programming-9/dealing-with-dates-in-a-text-file-935516/)

dunryc 03-20-2012 12:34 PM

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

Quote:

1, www.test.com Expiration Date:17-jun-2013
2, www.test.net Expiration Date:29-mar-2013
3, www.test.org Expiration Date:26-Jul-2012 04:00:00
4, www.test.biz Expiration Date:Mar 26 23:59:59 2012
5, www.test.info Expiration Date:27-Jul-2012 07:56:43

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

unSpawn 03-20-2012 12:43 PM

Just 'awk -F'Date:' '{print $NF}';' the time stamp I'd say because 'date' accepts input through "--date" so 'env TZ=UTC date --date="Mar 26 23:59:59 2012" +'%s';' works as does 'env TZ=UTC date --date="27-Jul-2012" +'%s' (for '%s' substitute your own format of course).

firstfire 03-20-2012 01:07 PM

Hi.

If you have GNU sed then
Code:

$ sed 's/\(.*Date:\)\(.*\)/echo "\1" `date -d "\2" +%d-%b-%Y`/e' infile.txt
1, www.test.com Expiration Date: 17-Jun-2013
2, www.test.net Expiration Date: 29-Mar-2013
3, www.test.org Expiration Date: 26-Jul-2012
4, www.test.biz Expiration Date: 26-Mar-2012
5, www.test.info Expiration Date: 27-Jul-2012

The 'e' sed command is a GNU extension. It pipes pattern space to the shell and replaces pattern space with the output. Remove `e' to see what commands are executed. The solution is not fast/effective but it is short.

dunryc 03-20-2012 04:19 PM

Quote:

Originally Posted by firstfire (Post 4631779)
Hi.

If you have GNU sed then
Code:

$ sed 's/\(.*Date:\)\(.*\)/echo "\1" `date -d "\2" +%d-%b-%Y`/e' infile.txt
1, www.test.com Expiration Date: 17-Jun-2013
2, www.test.net Expiration Date: 29-Mar-2013
3, www.test.org Expiration Date: 26-Jul-2012
4, www.test.biz Expiration Date: 26-Mar-2012
5, www.test.info Expiration Date: 27-Jul-2012

The 'e' sed command is a GNU extension. It pipes pattern space to the shell and replaces pattern space with the output. Remove `e' to see what commands are executed. The solution is not fast/effective but it is short.

thakns for that it works great firstfire would there be anyway to change the format again so i get instead of

Quote:

17-Jun-2013
Quote:

Jun-17-2013
cheers for the help :-)

catkin 03-20-2012 10:17 PM

Quote:

Originally Posted by dunryc (Post 4631934)
would there be anyway to change the format

Find out what +%d-%b-%Y does in the date command either via man date or via online examples like here.

hroptatyr 04-13-2012 05:21 AM

If you don't mind a non-standard utility you could use my dateutils

Specify all possible input dates with -i and the desired output format with -f. The -S stands for sed mode, to change the dates within the input line:
Code:

dconv -S -i '%d-%b-%Y %T' -i '%d-%b-%Y' -i '%b %d %T %Y' -f '%b-%d-%Y' <<EOF
1, www.test.com Expiration Date:17-jun-2013
2, www.test.net Expiration Date:29-mar-2013
3, www.test.org Expiration Date:26-Jul-2012 04:00:00
4, www.test.biz Expiration Date:Mar 26 23:59:59 2012
5, www.test.info Expiration Date:27-Jul-2012 07:56:43
EOF
=>
1, www.test.com Expiration Date:Jun-17-2013
2, www.test.net Expiration Date:Mar-29-2013
3, www.test.org Expiration Date:Jul-26-2012
4, www.test.biz Expiration Date:Mar-26-2012
5, www.test.info Expiration Date:Jul-27-2012



All times are GMT -5. The time now is 07:27 AM.