sed: -e expression #1, char 21: unterminated `s' command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: -e expression #1, char 21: unterminated `s' command
# 1  
Old 07-29-2011
sed: -e expression #1, char 21: unterminated `s' command

I have read many threads, but I still didn't find the right answer. May be i didn't find the right thread, though are so many threads for the same question.

Basically the situation is - find date in a file and replace it with another date. (its not homework, its part of lot of a big processing, so just extracting this piece of problem)
A file would contain records like this
john 2011/06/28 11:16:14
smith 2011/06/28 12:46:10
jessica 2011/06/28 06:16:40

I need to find john, replace the date on the same line with another date. I can find the required line, i am facing problem replacing the date. Here is the extracted code.

record="john 2011/06/28 11:16:14"
oldDate="2011/06/28 11:16:14"
newDate="2011/06/28 45:56:67"
echo $record | sed 's|'$oldDate'|'$newDate'|g'

Hence i get
sed: -e expression #1, char 21: unterminated `s' command

I got to know it has to do with the characters "\" & ":", but what exactly is the problem and what is the solution?
Can anybody explain me please?

---------- Post updated at 12:54 PM ---------- Previous update was at 12:48 PM ----------

Just figured out that I could replace the date and time separately, but not together. I think the space between the date and time is the problem.
Am I right?
# 2  
Old 07-29-2011
Try:
Code:
echo $record | sed 's|'"$oldDate"'|'"$newDate"'|'

I believe it's a shell's problem.
# 3  
Old 07-29-2011
I have tried almost all sorts of combinations, none of them have worked. Even this one didn't work.
I want to understand what's happening behind the scene.
# 4  
Old 07-29-2011
The below would not throw sed error posted by you.. but it neither matches the oldDate. Storing the pattern in a variable and trying to match it in Sed is IMO a bad idea. However you can try as
Code:
echo 'john 2011/06/28 11:16:14' | sed "s|$oldDate|$newDate|"


Last edited by michaelrozar17; 07-29-2011 at 06:04 AM.. Reason: after few more tries..
# 5  
Old 07-29-2011
Thanks yazu and michael for your inputs.
But it didn't work.

michael, as per your suggestion, if i dont have stored a pattern in a variable, then how to I search?
I will explain my problem
1. I get a name as input to the shell script.
2. Search for the name in a file (I have mentioned how the file look like in my first post)
3. Replace the old date with the new date

What was I trying to do it?
1. Have the new date and old date in variables
2. Replace the old string with the new string in the file

Now I have changed step 2 to
2. Delete the line which has the name
3. Insert a new line along with name and new date.
# 6  
Old 07-29-2011
Quote:
Originally Posted by michaelrozar17
The below would not throw sed error posted by you.. but it neither matches the oldDate. Storing the pattern in a variable and trying to match it in Sed is IMO a bad idea. However you can try as
Code:
echo 'john 2011/06/28 11:16:14' | sed "s|$oldDate|$newDate|"

Ok before that.. Does the above Sed produced any desired output of yours..?
# 7  
Old 07-29-2011
Quote:
Originally Posted by michaelrozar17
Ok before that.. Does the above Sed produced any desired output of yours..?
I output is same as input "john 2011/06/28 11:16:14"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed: -e expression #1, char 20: unterminated address regex

I am trying to add word in last of particular line. the same command syntex is running on prompt. but in bash script give error."sed: -e expression #1, char 20: unterminated address regex" Please help. for i in `cat servername`; do ssh -q -t root@$i sed -i '/simple_allow_groups =/s/$/,... (4 Replies)
Discussion started by: yash_message
4 Replies

2. Shell Programming and Scripting

Sed: -e expression #1, char 16: unterminated address regex

I am trying to grep for a particular text (Do action on cell BL330) in a text file(sample.gz) which is searched in the content filtered by date+timestamp (2016-09-14 01:09:56,796 to 2016-09-15 04:10:29,719) on a remote machine and finally write the output into a output file on a local machine. ... (23 Replies)
Discussion started by: rbadveti
23 Replies

3. Shell Programming and Scripting

Unterminated Regular Expression

It is ok when I send send one arguement to $TILL in the awk expression. first(){ TILL=$1 echo $TILL awk '/:\\/ {P=0} P {print $0 "<br>"} FNR==1{printf("File %s:<br>", FILENAME)} /'$TILL'\/ {P=1} ' ${dirlist } } echo "<table border = '1'>" echo '<tr><td> </td><td>' ;... (3 Replies)
Discussion started by: alvinoo
3 Replies

4. Shell Programming and Scripting

Sed: -e expression #1, char 2: extra characters after command

Greetings.. getting the error while execution of the script, correct where i am missing #!/bin/bash DATE=`date +%Y-%m-%d:::%H:%M` HOSTNAME=`hostname` TXT="/log/temp.txt" LOGPATH="/log1/commanlogs/" IP=`/sbin/ifconfig | grep -i inet| head -n1| awk '{print $2}'| awk -F : '{print $2}'`... (7 Replies)
Discussion started by: manju98458
7 Replies

5. Shell Programming and Scripting

sed returns error "sed: -e expression #1, char 18: unterminated `s' command"

Hello All, I have something like below LDC100/rel/prod/libinactrl.a LAA2000/rel/prod/libinactrl.a I want to remove till first forward slash that is outputshould be as below rel/prod/libinactrl.a rel/prod/libinactrl.a How can I do that ??? (8 Replies)
Discussion started by: anand.shah
8 Replies

6. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

7. UNIX for Dummies Questions & Answers

sed error unterminated `s' command

I have list of data I have cut down to format: I am using sed command to remove the sed 's/ Returns error: sed: -e expression #1, char 5: unterminated `s' command Full code line is: cat textFile | cut -d ' ' -f 4 | cut ':' -f 1 | sed 's/ Thanks, Please use next time code tags... (2 Replies)
Discussion started by: maximus73
2 Replies

8. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

9. Shell Programming and Scripting

sed unterminated `s' command?

Hi there, I'm pretty new to this whole scripting thing. I've written myself something which takes my txt file of SMSes (the backup from the phone), and puts them into an email format, saving them as .eml files. I've tested and uploaded a batch to gmail, but because of threading issues, I've... (1 Reply)
Discussion started by: donnacha
1 Replies

10. Shell Programming and Scripting

Using SED command in a shell script: Unterminated address regex

Hi All, I am trying to use a sed command in a shell script in order to delete some lines in a file and I got the following error message. I don't understand why it is not working 'cause I have tried with simple quotes, then with double-quotes, and it is not working. sed: -e expression #1,... (7 Replies)
Discussion started by: Alpha3363
7 Replies
Login or Register to Ask a Question