[Solved] sed - how replace date in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] sed - how replace date in script
# 1  
Old 11-27-2013
Question [Solved] sed - how replace date in script

Hi All,

I have a requirement to find and replace old date with new date value. Below is the scenario:

# In the input file, date is MM/DD/YYYY format

Code:
PREV_DTE=09/15/2013

I want to replace with 09/30/2013. It should look like

Code:
PREV_DTE=09/30/2013

I am using below sed command :

Code:
sed -e "s|PREV_DTE=|PREV_DTE=${PRE_DT_TO}|" < input_file > output_file

But the output i am getting is
Code:
PREV_DTE=09/15/201309/30/2013

Please help on how to achieve this !

Regards,

Last edited by Don Cragun; 11-27-2013 at 04:43 AM.. Reason: Add CODE tags.
# 2  
Old 11-27-2013
Hello,

It's a kind request please use code tags while posting queries.
Here is the following which may help you.

Code:
echo "09/15/2013" | awk -v new_date="09/30/2013" '(sub($0,new_date))'
09/30/2013


Thanks,
R. Singh
# 3  
Old 11-27-2013
Try:

Code:
$ date -d"09/15/2013 + 15 day" +%m/%d/%Y
09/30/2013

Code:
$ echo "09/15/2013" | awk '$1=Newdate' Newdate="09/30/2013"
09/30/2013

# 4  
Old 11-27-2013
Try:
Code:
sed -e "s|PREV_DTE=../../....|PREV_DTE=${PRE_DT_TO}|" < input_file > output_file

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-27-2013
Thanks Don Cragun ! It worked...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

2. Shell Programming and Scripting

sed command to replace slash in date format only

Hello experts. I haven't been able to find a solution for this using the sed command. I only want to replace the forward slash with string "FW_SLASH" only if there's a number right after the slash while preserving the original number. I have a file containing 2 entries: Original File:... (5 Replies)
Discussion started by: pchang
5 Replies

3. Shell Programming and Scripting

[Solved] sed to replace words

Hello All, I have file named filelist.txt a.bteq.ctl b.bteq.ctl c.bteq.ctl I want to replace the word bteq to tpt in this file. I used this sed command cat filelist.txt | sed 's/bteq/tpt/g' > filelist.txt But this command deletes all records from the filelist.txt Can... (2 Replies)
Discussion started by: nnani
2 Replies

4. Shell Programming and Scripting

sed convert date/replace

Hi, I wold like to replace this 2012 Mar 02 to this 20120302. i have already this code: ls -lrt | awk '{ print $8$6$7 }'| sed -e s/*:*/'2013'/g -e s/'Jan'/01/g -e s/'Feb'/02/g -e s/'Mar'/03/g -e s/'Apr'/04/g -e s/'May'/05/g -e s/'Jun'/06/g -e s/'Jul'/07/g -e s/'Aug'/08/g -e... (7 Replies)
Discussion started by: snayper
7 Replies

5. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

6. Shell Programming and Scripting

Replace value of a variable in a file through script using sed

Hi, I am trying to replace the value of a variable in a file through another script. Example: Filename : abc.txt contents: a=10 b=20 c=30 Need to change the value of, say, b - Tried using the following: sed "s/${b}/15/g" abc.txt Have tried various forms of sed (with single quotes,... (4 Replies)
Discussion started by: rituparna_gupta
4 Replies

7. Shell Programming and Scripting

Using sed command replace date variable in unix

I need to use a shell script, using sed command how to replace date variable value in following format. 04/18/2012 11:38:55 Because the sed is treating the '/' as a parameter instead of the value of a variable, and hence there is the message as sed: command garbled: s/insert/04/18/2012... (9 Replies)
Discussion started by: jannusuresh
9 Replies

8. Shell Programming and Scripting

[Solved] sed : last character replace

Hi all , I have to write a shell script that takes a number as input , like 123 and the output will be 6 ,i.e the output will be the sum of digits of the input. I have an idea as follows, echo "123"|fold -1|tr '\n' '+'|bc But the problem is after " echo "123"|fold -1|tr '\n' '+' "... (5 Replies)
Discussion started by: M.Choudhury
5 Replies

9. Shell Programming and Scripting

Solved - Sed - Positional Replace

Hi All, I don't know what I am doing wrong in the regex below. I have the following string: 31000000010201005181121360000000100000000003000000YYY-YYY-YYY-20100518-104139.txt.YYY I need to split it in parts: - Group 1: 3100000001020100518112136 - Group 2: 000000010 - Group 3:... (0 Replies)
Discussion started by: felipe.vinturin
0 Replies

10. Shell Programming and Scripting

how can i replace / with new line in shell script or sed ?

1. how can i replace ' / ' with new line in shell script or sed ? 2. how can set a conditon untill null in while loop while ( i== null ) do ...... done (3 Replies)
Discussion started by: mail2sant
3 Replies
Login or Register to Ask a Question