searching a date and replacing with another date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching a date and replacing with another date
# 1  
Old 10-29-2009
searching a date and replacing with another date

I have a text file that i want to search through and pick out any dates that are formatted like MM/DD/YYYY and replace them with a date i want like 10/29/2009. any idea show i would do this?Smilie

Snapshot of my text file:
Code:
test4>s44syd5172>070>528>ENU>nongnuan>wanrawee>sr2330532>
THC>ms528enu>8>AB>>U11.0>10/26/2552>14:49:49>10/26/2552>16:01:53>
4317>0>871.00>36/6/0/p>8>1AB>4/1/0/p>80.00>>>>>>2AB>6/1/0/p>85.00>
>>>>>3AB>6/0/0/p>100.00>>>>>>4AB>6/1/0/p>85.00>>>>>>5AB>6/0/0/p>

Need to search and replace the highlighted text above with another date. Occurance will anywhere and many times in a text file .

Thanks
Please reply me ASAPSmilie

---------- Post updated at 04:31 AM ---------- Previous update was at 04:03 AM ----------

I dont want to update time. need only date

Last edited by Franklin52; 10-29-2009 at 07:33 AM.. Reason: Please use code tags!
# 2  
Old 10-29-2009
Code:
sed 's:[0-1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]:10/18/2009' file_name

Try this...
# 3  
Old 10-29-2009
thanks but it is not working
sed: fatal error: Unrecognized command: s:[0-1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]:10/18/2009 Smilie
# 4  
Old 10-29-2009
am sorry about the syntax i gav...

Code:
sed 's:[0-1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]:10/18/2009:g' file_name


Last edited by Franklin52; 10-29-2009 at 07:33 AM.. Reason: Please use code tags!
# 5  
Old 10-29-2009
Thank u so much sir. this is working now.
:-)
Would really appreciate if you change this script for current date.
means instead of 10/18/2009 i want current date automatically. Smilie
# 6  
Old 10-29-2009
try this:

Quote:
sed 's:[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]:'`date +%m/%d/%y`':g' file

Last edited by Franklin52; 10-29-2009 at 07:34 AM.. Reason: Please use code tags!
# 7  
Old 10-29-2009
its giving error
date error: Syntax: date [ mm-dd-yy ] [ hh:mm:ss ]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

2. Shell Programming and Scripting

Replacing Date in the file with Create date and timestamp

Hello, I have files that with a naming convention as shown below. Some of the files have dates in the file name and some of them don't have dates in the file name. imap-hp-import-20150917.txt imap-dell-gec-import-20150901.txt imap-cvs-import-20150915.txt imap-gec-import.txt... (8 Replies)
Discussion started by: Saanvi1
8 Replies

3. Shell Programming and Scripting

Searching for unknown date inside the file and replace to new date

Hello, Iam a newbies to Shell scripting. Iam trying to replace the date inside the file to new date. is there anyway that we can just use the pattern to search as "..." I have many files want to replace with the same date, and each file contains different date. Thanks for your help. ... (2 Replies)
Discussion started by: Daro
2 Replies

4. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

5. Shell Programming and Scripting

Replacing / by - in date

Dear Friends, Following is manupulated output of our script. e.g. lkme_lpst 2 Pur_dt 31/12/2011 bl_dt 01/02/2011 rt_dt 02/02/2011 prod_btch 19/1452147-5210 We further want to manupulate it and want to replace / by - i.e. Lkme_lpst 2 Pur_dt 31-12-2011 bl_dt 01-02-2011... (7 Replies)
Discussion started by: anushree.a
7 Replies

6. UNIX for Dummies Questions & Answers

Searching by date range from filenames

Hello all, i have tons of files in folder named like this (yyyymmdd): bookcollection20100729 bookcollection20100730 bookcollection20100731 bookcollection20100801 bookcollection20100802 etc. I need to find files with date range in there names lets say from 2010.07.30 - 2010.08.02 ... (10 Replies)
Discussion started by: Whit3H0rse
10 Replies

7. UNIX for Dummies Questions & Answers

Searching the date pattern in a file

Hi, I would like to search the pattern based on the date like "2010/08/15". I tried using / in the file giving /<<pattern>>. when i tried this it turns to /2010/+8, but not going to the pattern what ever i want. This is how the data in the file. INFO | jvm 1 | 2010/05/26 13:30:33... (5 Replies)
Discussion started by: venkatesht
5 Replies

8. Shell Programming and Scripting

Issue searching for Date

I have an issue regarding searching for old dates. At the moment I search a field within a file for the last 3 days from the system date: using today as 25/02/2010 DD=$(expr `date +%d` - 1) DD1=$(expr `date +%d` - 2) DD2=$(expr `date +%d` - 3) MTH=`date +"%m"` YR=`date +"%Y"`... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

9. Shell Programming and Scripting

sed replacing date

I found this code on here in another thread (which I can't find now). The code works on normal text, but not for a date in the format of "mm/dd/yyyy". I assume it has to do with the "/". I am using korn shell. Any ideas? sed 's/${OldDate}/${NewDate}/g' < file > file.new mv file.new file (2 Replies)
Discussion started by: stringzz
2 Replies

10. UNIX for Dummies Questions & Answers

searching a file by date...

It's possible to use "find" to search a file that was modified for example between 5/10/2004 and 7/11/2005? How can i do this? I saw there is option -mtime, but i don't understand how to use it in this case. Thanks (4 Replies)
Discussion started by: Kaminski
4 Replies
Login or Register to Ask a Question