Replacing / by - in date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing / by - in date
# 1  
Old 03-19-2011
Replacing / by - in date

Dear Friends,

Following is manupulated output of our script.

e.g.
Code:
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.
Code:
Lkme_lpst 2
Pur_dt 31-12-2011
bl_dt   01-02-2011
rt_dt   02-02-2011
prod_btch 19/1452147-5210

at the same time we dont want to convert / to - where its not a date (like in earlier example product_batch)

Please guide us to do the same.
Thanks
Anushree

Last edited by Franklin52; 03-21-2011 at 05:01 AM.. Reason: Please use code tags
# 2  
Old 03-19-2011
How about this?
Code:
perl -nle 's#(\d{2})/(\d{2})/(\d{4})#$1-$2-$3#g;print;'  inputfile

OR
Code:
sed 's#\(.*\)/\(.*\)/\(.*\)#\1-\2-\3#g' inputfile

# 3  
Old 03-19-2011
Dear Pravin,

tried 1st solution and it worked without any changes.

Thanks a lot for quick reply.
Take care

Anu.
# 4  
Old 03-19-2011
Code:
awk '{gsub(/\//,"-",$2)}1' infile

# 5  
Old 03-19-2011
Hey Friends,
Sorry forgot to mention one imp thing

Now as suggested I am using

Code:
perl -nle 's#(\d{2})/(\d{2})/(\d{4})#$1-$2-$3#g;print;' INPUTFILE

to get desired output.

But I forgot to mention that I want to convert Date format from MM-DD-YYYY to DD-MM-YYYY.

Please guide.

Anu.
# 6  
Old 03-19-2011
Change the captured group. like below.
Code:
 perl -nle 's#(\d{2})/(\d{2})/(\d{4})#$2-$1-$3#g;print;' inputfile

# 7  
Old 03-19-2011
Wow Pravin, it worked.
Thanks a tonnn...
God bless you.

Anushree.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

2. 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

3. 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

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 date with a variable in a file

Hi, I've a variable for example.. ACTIVATION_DATE=2010-11-11 (the date above is a result of a sql query and not hardcoded) now there is another file (test_2.parm) where there are many variables predefined.. REG_CODE=111 ACT_DATE=2010-10-10 CAN_DATE=8888-31-12 Now I want to search for... (1 Reply)
Discussion started by: RRVARMA
1 Replies

6. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

7. Shell Programming and Scripting

Renaming a file and replacing the special character in the name with date

HI all, How can i rename some files and replace the special character in the name with todays date ex: Name#file1.txt Name#file2.txt to be renamed as Name.20091119.file1.txt Name.20091119.file2.txt (11 Replies)
Discussion started by: abhinav192
11 Replies

8. Shell Programming and Scripting

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?:) Snapshot of my text file: test4>s44syd5172>070>528>ENU>nongnuan>wanrawee>sr2330532>... (7 Replies)
Discussion started by: infiant
7 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. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question