Replacing the Dates in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing the Dates in a file
# 1  
Old 02-19-2014
Replacing the Dates in a file

Hello Gurus,

I'm beginner in Shell scripting.
I got a requirement to write a script.

I have a file with below (similar) content

Quote:
Raheem: 20Jun1985; Keeth: 23Dec2013
Rakesh: 34Hgg4233 L009334622fsda
Harsha: 19Jan1999 9849043244 Venkat: 04Mar2000
LR224HYZA245; Ramu: 29Feb2011
If you can observe above content, there are many date values existed (with different dates) in a format: ddMonyyyy

I have to write replace all these dates with another format: DD-MON-YY

I'm trying it to do in a single line command, but felt bit complexity.
Hence trying for shell script for same.
Could anyone please help me on above?

Thanks in advance.
VRN
# 2  
Old 02-19-2014
For posted input, try:
Code:
sed 's#\([0-9][0-9]\)\([A-Z][a-z][a-z]\)\([1-2][0-9]\)\([0-9][0-9]\)#\1-\2-\4#g' file

Note that this will not work for all scenarios. But I guess it will give you an idea to come up with something of your own.
# 3  
Old 02-19-2014
Hi Yoda,

Thanks much for your reply.

Yeah, I tried in the same way only.... But there should be an accurate way for replacing the dates only.

If you notice the file content contains another word
Quote:
Rakesh: 34Hgg4233
which is similar to date format, but not the date.

I have to omit such kind of keywords also in case file contains in any line.

Could someone suggest me the exact pattern which can accomplish the task pls?

Thanks in advance...

VRN
# 4  
Old 02-19-2014
Here is an awk solution:

Code:
awk 'BEGIN{
   r= "[0-9][0-9](Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Nov|Dec)[12][0-9][0-9][0-9]"
}
$0 ~ r {
while(match($0,r)){
   printf "%s-%s-%s",substr($0,1,RSTART+1),
    toupper(substr($0, RSTART+2, 3)),
    substr($0, RSTART+5,RLENGTH-5);
        $0=substr($0,RSTART+RLENGTH);
}}1 ' infile

# 5  
Old 02-19-2014
Try :

add/remove comments shown in code according to your need

Code:
awk 'BEGIN{
            m="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
            for(i=1;i<=split(m,A);i++)Mon[A[i]]=sprintf("%02d",i)
          }
          {
		for(i=1;i<=NF;i++){
                        for(j in Mon){
                                       # example 01102012
                                       # if(gsub(j,Mon[j],$i))break
				       
				       # example 01-10-2012
				       # if(gsub(j,"-"Mon[j]"-",$i))break

				       # example 01-Oct-10 
				       if(gsub(j,"-"j"-",$i))
				       {
					 $i = substr($i,1,length($i)-4) substr($i,length($i)-1)
                                         break
                                       }
                                     }
                                  }
          }1' file

# 6  
Old 02-20-2014
Thanks much Akshay/Chubler for your solution..

Could this be done with any unix command except AWK... because my team is not much familiar with AWK (in case of any future modifications)?

Thanks again...
# 7  
Old 02-20-2014
You can use sed:

Code:
$ sed -r 's/([0-9][0-9])(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Nov|Dec)[12][0-9]([0-9][0-9])/\1-\U\2-\3/g' infile
Raheem: 20-JUN-85; Keeth: 23-DEC-13
Rakesh: 34Hgg4233 L009334622fsda
Harsha: 19-JAN-99 9849043244 Venkat: 04-MAR-00
LR224HYZA245; Ramu: 29-FEB-11


Last edited by Chubler_XL; 02-20-2014 at 12:33 AM..
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Grep file containing dates

How to grep a file containing dates to only last 30 days then move to another folder (7 Replies)
Discussion started by: kmarcus
7 Replies

2. UNIX for Advanced & Expert Users

How to get the Missing dates between two dates in the table?

Hi Am Using Unix Ksh ... I have a Table called date select * from date ; Date 01/02/2013 06/02/2013 I need the output as Missing Date 01/02/2013 02/02/2013 03/02/2013 04/02/2013 05/02/2013 06/02/2013 (2 Replies)
Discussion started by: Venkatesh1
2 Replies

3. UNIX for Dummies Questions & Answers

How to write the dates between 2 dates into a file

Hi All, I am trying to print the dates that falls between 2 date variables into a file. Here is the example. $BUS_DATE =20120616 $SUB_DATE=20120613 Output to file abc.txt should be : 20120613,20120614,120120615,20120616 Can you pls help me accomplish this in LINUX. Thanks... (5 Replies)
Discussion started by: dsfreddie
5 Replies

4. Emergency UNIX and Linux Support

Replacing dates]] with (dates)]]

Hi guys, For my wiki site I need to fix 1400 pages that use the wrong date format, most pages (not all) use eg. 1988]] I need to change that to (1988)]] The date range goes back to 1400 so I guess I need to do the following ssh into my server, dump mysql database vi .sql dump search... (20 Replies)
Discussion started by: lawstudent
20 Replies

5. Shell Programming and Scripting

lines between two dates in a file

Hi Every body I had a data in file as shown.. Now i want to get the data and insert into other file but between the two date ranges or from a past date to current date..Please Guide me in doing that.. Below is the data that looks like... child process 2588 still did not exit, sending a... (2 Replies)
Discussion started by: Reddy482
2 Replies

6. Shell Programming and Scripting

How to processing the log file within certain dates based on the file name

Hi I am working on the script parsing specific message "TEST" from multiple file. The log file name looks like: N3.2009-11-26-03-05-02.console.log.tar.gz N4.2009-11-29-00-25-03.console.log.tar.gz N6.2009-12-01-10-05-02.console.log.tar.gz I am using the following command: zgrep -a --text... (1 Reply)
Discussion started by: shyork2001
1 Replies

7. UNIX for Dummies Questions & Answers

Extract dates from file name.

Hi All, I have files with names as us_Gec1_wk_01to01_2008.TXT ad_EngEnt_wk_01to10_2008.TXT br_EngMov_wk_01to10_2008.TXT Over here, I need to extract the dates and the year and store them in variables. How can I achieve the same in bash. In case of ad_EngEnt_wk_01to10_2008.TXT ... (3 Replies)
Discussion started by: Swapna173
3 Replies

8. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Shell Programming and Scripting

Help to switch dates from a file

Hi. I need some assistance with a file who at the end i must import to EXCEL. The problem is that i have a file with this inside: Output: JOBID START TIME END TIME ELAPSED CPU ------------------------------------------------------------ AVERAGE: ... (2 Replies)
Discussion started by: osramos
2 Replies

10. UNIX for Dummies Questions & Answers

Formatting dates in a file

Hi! Sitting with a headache this morning and can't get the brain around it! I have a file with 32000 lines that needs to be inserted onto a database. My problem is with the following: First part between bars is a date and second some identifiers |19511108|0001417| |19481024|0001439|... (6 Replies)
Discussion started by: maverick
6 Replies
Login or Register to Ask a Question