LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2012, 12:34 PM   #1
dunryc
LQ Newbie
 
Registered: Jul 2004
Posts: 10

Rep: Reputation: 0
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
 
Old 03-20-2012, 12:43 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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).
 
Old 03-20-2012, 01:07 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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.
 
Old 03-20-2012, 04:19 PM   #4
dunryc
LQ Newbie
 
Registered: Jul 2004
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by firstfire View Post
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 :-)
 
Old 03-20-2012, 10:17 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by dunryc View Post
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.
 
Old 04-13-2012, 05:21 AM   #6
hroptatyr
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to processing the log file within certain dates based on the file name shyork2001 Linux - General 1 04-08-2010 03:35 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
File Dates Rv5 Programming 5 09-07-2004 05:59 PM
File dates. vexer Programming 5 04-28-2004 12:14 AM
Dealing with text in bash scripts Skute Programming 6 03-16-2004 02:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration