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
# 8  
Old 04-19-2012
I meant this:

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

# 9  
Old 04-19-2012
Quote:
Originally Posted by Epsilon
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.
you can use the different delimiters/separator in the almost all of the seds.(all available for GNU sed but forget the special means of thats for shells.)
but we prefer / delimiter for more readable..
but if your string contains same chars with sed's delimeter then we can try change it.
some ones ,:|;#@!%

if u can use "/" then you have to escape the slahs
Code:
# sed "s/insert/$(echo ${STR1//\//\\\/})/g" file


and usage single/double quotes may like this in te sed expr.

--> double quotes usefull when expand variable (especially variable contains special meaning variables [white spaces,slashes,the other variables with back slahes..)
Code:
sed "s|insert|$STR1|g" file

--> single quotes usefull when our variable does not contain these chars but we have to use double quotes at the front of the STR1 because of escape for these special variables..
Code:
sed 's;insert;'"$STR1"';g' file

--> if the STR1="04/18/2012" may we can use only single quotes, because of STR1 is a string without space
Code:
# sed 's|insert|'$STR1'|g' file


regards
ygemici

Last edited by ygemici; 04-19-2012 at 11:23 AM..
# 10  
Old 04-19-2012
Thanks ygemici(sed_shell@LNU)

The following script worked well for me.
Code:
sed "s#insert#insert = $STR1#g" filename

resulting as
Code:
insert = 04/18/2012 11:38:55

Thanks
Suresh

---------- Post updated at 07:56 PM ---------- Previous update was at 07:42 PM ----------

I tested sed "s/insert/$(echo ${STR1//\//\\\/})/g" file
as well, working fine to me. Giving the same result as sed "s#insert#$STR1#g" file

Thanks to ygemici & all.
Thanks|Suresh

---------- Post updated at 09:06 PM ---------- Previous update was at 07:56 PM ----------

Hi,
Thanks for your previous posts. I have another requirement with little different output. The sed "s#IncludeDateTime =#IncludeDateTime =$STR1#g" file1.
This is appending the new value to the existing value. what I want is the existing value removed and assign new value. eg. $$IncludeDateTime =04/18/2012 11:38:55 01/01/2011 10:55:37. So what I want from the above command is $$IncludeDateTime =04/18/2012 11:38:55 and should not append the exising value 01/01/2011 10:55:37.

Or exising entire line should be removed and new line to be added with the new results.

Can any one please help in this regard.
Heartful Thanks to all in advance. Thanks | Suresh

Last edited by Scrutinizer; 04-19-2012 at 11:15 AM.. Reason: code tags
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