LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-05-2015, 08:44 PM   #1
lonesoac0
Member
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 101

Rep: Reputation: 4
Date Formatting


Hello all,

I am interested in re-formatting some date values from:

JAN 18-19
MAR 21-22
FEB 28-MAR 1
AUGUST 8-9

To:

JAN 18-JAN 19
MAR 21-MAR 22
FEB 28-MAR 1
AUGUST 8-AUGUST 9

Any ideas in how to do this?
 
Old 03-05-2015, 09:18 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Something with a bit of logic - awk, perl, python.
Say in awk, use (white-)space and "-" as field separators, check if the third field is numeric, if so, plug the first field in before it, else use the current record as-is.
 
Old 03-05-2015, 09:50 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
JAN 18-19
MAR 21-22
FEB 28-MAR 1
AUGUST 8-9
... this awk ...
Code:
awk '{gsub("-","- ",$0)
   if (NF==3) print $1,$2$1,$3
         else print $1,$2$3,$4 }' $InFile >$OutFile
... produced this OutFile ...
Code:
JAN 18-JAN 19
MAR 21-MAR 22
FEB 28-MAR 1
AUGUST 8-AUGUST 9
Daniel B. Martin
 
Old 03-05-2015, 10:25 PM   #4
lonesoac0
Member
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 101

Original Poster
Rep: Reputation: 4
I cannot WAIT to try it out tmw! I will let you know!
 
  


Reply

Tags
awk, bash, sed



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
[SOLVED] bash: setting date with a custom formatting RudyMartin Linux - Newbie 1 12-01-2011 03:53 PM
date formatting from stat command tensigh Programming 10 08-30-2010 04:13 AM
PHP: formatting current date & time as: 2006-04-12 13:47:36 and substract 15 minutes NaCo Linux - Software 4 11-13-2008 07:31 PM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
formatting date output grob115 Linux - Newbie 2 01-28-2006 08:03 AM

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

All times are GMT -5. The time now is 01:20 PM.

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