sed does not allow date command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed does not allow date command
# 1  
Old 09-20-2010
sed does not allow date command

Hi,

I wanted to perform a substitution using 'sed' in my script. I issue the below command

sed -i 's/DATE_TIME = .*/DATE_TIME ='`date`'/' file1.txt

but it fails with the below error:
sed: -e expression #1, char 43: unterminated `s' command

using a debug option in my script i got this

+ sed -i 's/DATE_TIME = .*/DATE_TIME = Mon' Sep 20 08:16:36 IST '2010/ file1.txt

the single quote(') after "Mon" and before "2010" above is creating an issue here.

Can anybody help me out?

Thanks,
Neil
# 2  
Old 09-20-2010
Don't forget to use code tags

change ' to "

Code:
sed -i "s/DATE_TIME = .*/DATE_TIME =`date`/" file1.txt

# 3  
Old 09-20-2010
There's a bit more than just changing each single quote to double quotes. You'll need to drop the double quotes in the middle:

Code:
sed -i "s/DATE_TIME = .*/DATE_TIME =`date`/" file1.txt

What was happening was the tokens generated by the date command were treated as multiple tokens on the sed command; everything up through the day of week token was interpreted as the 'programme' (it was the first token on the command line), and the rest were going to be interpreted as file names. By supplying the whole sed programme in double quotes, you allow the command in back quotes to be evaluated and the result substituted, while including them as a part of the quoted string.

As a side note, I believe that $(command) is now preferred to using backquotes.
This User Gave Thanks to agama For This Post:
# 4  
Old 09-20-2010
or you could also try this

Code:
sed -i 's/DATE_TIME = .*/DATE_TIME ='"`date`"'/' file1.txt

This User Gave Thanks to 116@434 For This Post:
# 5  
Old 09-20-2010
Hummm, I swore that rdcwayx's post didn't include an example... Oh well, sorry for the duplicate information.
# 6  
Old 09-20-2010
Thanks guys.. it works exactly the way i want .

Thanks a ton Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk command to convert number occurances into date format and club a set of lines

Hi, I have been stuck in this requirement where my file contains the below format. 20150812170500846959990854-25383-8.0.0 "ABC Report" hp96880 "4952" 20150812170501846959990854-25383-8.0.0 End of run 20150812060132846959990854-20495-8.0.0 "XYZ Report" vg76452 "1006962188"... (6 Replies)
Discussion started by: Chinmaya Kabi
6 Replies

2. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

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

4. Shell Programming and Scripting

Display date in mm/dd/yy format in sed command

Hi All, Following is my issue. $MAIL_DOC = test.txt test.txt contains the following text . This process was executed in the %INSTANCE% instance on %RUNDATE%. I am trying to execute the following script var=`echo $ORACLE_SID | tr ` NOW=$(date +"%D") sed -e... (3 Replies)
Discussion started by: megha2525
3 Replies

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

6. Shell Programming and Scripting

Calculating expiry date using date,sed,grep

Hi, I would greatly appreciate it if someone can help me with my problem. I have a crawler which collects spam URLs everyday & this data needs to be published in a blacklist. Here's the catch: The "Time To Live" (TTL) for each URL is 3 months (or whatever for that matter). If i see the... (5 Replies)
Discussion started by: r4v3n
5 Replies

7. Shell Programming and Scripting

Filter date and time form a file using sed command

I want to filter out the date and time from this line in a file. How to do this using sed command. on Tue Apr 19 00:48:29 2011 (12 Replies)
Discussion started by: vineet.dhingra
12 Replies

8. Shell Programming and Scripting

Format of SED command to change a date

I have a website. I have a directory within it with over a hundred .html files. I need to change a date within every file. I don't have an easy way to find/replace. I need to change 10/31 to 11/30 on every single page at once. I tried the command below but it didn't work. Obviously I don't know... (3 Replies)
Discussion started by: ijustsawmars
3 Replies

9. Shell Programming and Scripting

Separate date timestamp use awk or sed command ?

Hi, I have logfile like this : Actually the format is date format : yyyymmddHHMMSS and i want the log become this format yyyy-mm-dd HH:MM:SS for example 2009-07-19 11:46:52 Can somebody help me ? Thanks in advance (3 Replies)
Discussion started by: justbow
3 Replies

10. Shell Programming and Scripting

sed command using the DATE value in Oracle

Hi all, I am using the SQL Loader in Oracle for my data loading. So i create dynamic control script in unix. I replace the variables with input data in control file using the sed command. The sed commands works fine for the character data, but gives me error on the DATE value. sed ... (1 Reply)
Discussion started by: utham
1 Replies
Login or Register to Ask a Question