Using sed command replace date variable in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed command replace date variable in unix
# 1  
Old 04-19-2012
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
Code:
sed: command garbled: s/insert/04/18/2012

PLease help me as I am new to this scripting.

Thanks
Suresh

Last edited by Scrutinizer; 04-19-2012 at 10:24 AM.. Reason: code tags
# 2  
Old 04-19-2012
Use another separator, eg:

Code:
sed 's!<pattern>!04/18/2012 11:38:55!'

# 3  
Old 04-19-2012
Thanks Franklin for the reply.

Well, the actual command I use in the script is like this..

Code:
sed "s/insert/'$STR1'/g" filename

where insert should be replaced with the value of the parameter 'STR1' which is eg. 04/18/2012 11:38:55

I am getting the following error message.

Code:
sed: command garbled: s/insert/'04/18/2012 11:38:55'/g

Hence could you please tell me exactly how can I use it in the script. Thanks in advance, Suresh.
Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them
# 4  
Old 04-19-2012
also this:

i am agree with Franklin52.You can use any character that is not used in the entire expression for any other purpose as a separator instead of slash /.But there is another way here-
shell can be made to ignore special meaning of embedded slashes and treat them as literals by using backslash.So,use \ for each separating slash in date field like dd\/mm\/yyyy.
# 5  
Old 04-19-2012
Quote:
Originally Posted by jannusuresh
Thanks Franklin for the reply.

Well, the actual command I use in the script is like this..

Code:
sed "s/insert/'$STR1'/g" filename

where insert should be replaced with the value of the parameter 'STR1' which is eg. 04/18/2012 11:38:55

I am getting the following error message.

Code:
sed: command garbled: s/insert/'04/18/2012 11:38:55'/g

Hence could you please tell me exactly how can I use it in the script. Thanks in advance, Suresh.
try like this Smilie
Code:
# sed "s#insert#$STR1#g"

# 6  
Old 04-19-2012
the correct format of using the substitution command in sed is:
Code:
sed 's/pattern1/pattern2/g' filename

and inside '....' for variable evaluation use "$variable".

I hope the error should not come now.

Last edited by Franklin52; 04-19-2012 at 10:52 AM.. Reason: Please use code tags for data and code samples
# 7  
Old 04-19-2012
You cannot substitute variables inside single quotes, must be double quotes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

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

3. Shell Programming and Scripting

[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 PREV_DTE=09/15/2013 I want to replace with 09/30/2013. It should look like PREV_DTE=09/30/2013 I am using below sed command :... (4 Replies)
Discussion started by: rockygsd
4 Replies

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

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

6. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

7. Shell Programming and Scripting

Replace with a variable in sed command

Hello, I have this command and it works fine. My question is that how can we replace the N by a variable, to print for instance a big number of lines. It means if I want 100 lines after an expression, to not put "N" 100 times in the sed. Code: $ sed -n '/aaa/{n;N;N;s///g;s/;/; /g;p;}'... (2 Replies)
Discussion started by: rany1
2 Replies

8. Shell Programming and Scripting

using file-and-replace sed command with the use of a variable?

Ok, so, let's say I have the variable $GMAILID....How can I use it with sed command so to replace a string in a file? e.g.: sed -i 's/$GMAILID/test@gmail.com/' /home/$USER/Desktop/sendmail (4 Replies)
Discussion started by: hakermania
4 Replies

9. Shell Programming and Scripting

invoke unix variable in SED command

Hi, I am trying tio invoke unix variable in a sed command like below, but it seems to be failing.. a=1 sed -n '$a,$p' file.txt ### Failing but a=1 sed -n '1,$p' files.txt ### Works fine Please help me to fix this... Thanks in advance (2 Replies)
Discussion started by: vjayraghavan
2 Replies

10. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies
Login or Register to Ask a Question