LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2020, 08:59 AM   #1
MattMorris1
LQ Newbie
 
Registered: Jul 2020
Posts: 2

Rep: Reputation: Disabled
date change mm/dd/yyyy to dd/mm/yyyy


Hi,

I have files with dates only in them
08/10/2020
09/10/2020
10/10/2020
11/10/2020
12/10/2020

need to change them to DD/MM/YYYY

10/08/2020
10/09/2020
10/10/2020
10/11/2020
12/10/2020

whats the best way please
 
Old 07-10-2020, 09:10 AM   #2
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 4,989

Rep: Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488Reputation: 1488
The go-to tools for that kind of substitution are sed and awk. Since your string format is simple, sed should work, for example:
Code:
sed 's#^\(..\)/\(..\)#\2/\1#' inputfile > outputfile
The modified dates are in the outputfile.

How it works: THe hash character # separates the original string from the substituted string. The ^ means "beginning of line". A dot stands for any character. Two dots enclosed in \( and \) are two characters from the original string that are remembered and used in the substitution string. \1 is the first remembered two characters, and \2 the second. The slash means slash.

Last edited by berndbausch; 07-10-2020 at 09:13 AM.
 
2 members found this post helpful.
Old 07-10-2020, 09:11 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 1,140

Rep: Reputation: Disabled
berndbausch already said it all, so this here is just for fun. There is an interesting tool called fex (Field Extractor) which is somewhat of a crossover between cut and awk, with a very concise syntax:
Code:
fex '/{2,1,3:}' <inputfile >outputfile

Last edited by shruggy; 07-10-2020 at 12:42 PM.
 
1 members found this post helpful.
Old 07-10-2020, 09:15 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,964

Rep: Reputation: 6375Reputation: 6375Reputation: 6375Reputation: 6375Reputation: 6375Reputation: 6375Reputation: 6375Reputation: 6375Reputation: 6375Reputation: 6375Reputation: 6375
Quote:
Originally Posted by MattMorris1 View Post
Hi,

I have files with dates only in them
08/10/2020
09/10/2020
10/10/2020
11/10/2020
12/10/2020

need to change them to DD/MM/YYYY

10/08/2020
10/09/2020
10/10/2020
10/11/2020
12/10/2020

whats the best way please
This sounds suspiciously like a homework question. What have you done/tried so far? Where are you stuck??? Read the "Question Guidelines" link in my posting signature..we're happy to help, but you have to show some effort of your own.

If this is a real-world problem, I'd first ask where those dates are coming from, and what you're trying to do with them. For example, MySQL has a STR_TO_DATE function that will do some things for you. Perl has Time::Piece which will convert things as well, and Python also has similar routines. The 'best' way depends on what you're getting IN, from where, and what you want to do with it afterwards.
 
1 members found this post helpful.
Old 07-10-2020, 10:17 AM   #5
MattMorris1
LQ Newbie
 
Registered: Jul 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
The go-to tools for that kind of substitution are sed and awk. Since your string format is simple, sed should work, for example:
Code:
sed 's#^\(..\)/\(..\)#\2/\1#' inputfile > outputfile
The modified dates are in the outputfile.

How it works: THe hash character # separates the original string from the substituted string. The ^ means "beginning of line". A dot stands for any character. Two dots enclosed in \( and \) are two characters from the original string that are remembered and used in the substitution string. \1 is the first remembered two characters, and \2 the second. The slash means slash.
thanks - that worked a treat
 
  


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
Unable to subtract current date and future date in YYYY-MM-DD format archie7 Linux - Newbie 11 05-11-2018 03:29 AM
[SOLVED] How do I change the System Date format to DD/MM/YYYY on RHEL Workstatio 6.1 skinutter Linux - Newbie 3 09-28-2011 08:07 AM
[SOLVED] SOLVED: awk: d/m/yyyy h:mm to yyyy-mm-dd hh:mm:ss geetalicious Programming 2 05-05-2011 03:18 AM
[SOLVED] datetime format from MM-DD-YYYY HH:MM:SS to YYYY-MM-DD HH:MM:SS hattori.hanzo Programming 4 10-13-2010 07:04 PM
why does "ls -l" shows date as YYYY-mm-dd? joroxx Slackware - Installation 2 11-17-2004 10:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:18 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