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?

Try:

echo $record | sed 's|'"$oldDate"'|'"$newDate"'|'

I believe it's a shell's problem.

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.

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

echo 'john 2011/06/28 11:16:14' | sed "s|$oldDate|$newDate|"

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.

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"

It does work for me in GNU Sed. What sed you have..? sed --version

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

[quote=michaelrozar17;302542960]
It does work for me in GNU Sed. What sed you have..?
my sed version is GNU sed version 4.1.5

Do you try your extracted code or you just change your script?

% cat <script.sh
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"'|'

% sh script.sh
john 2011/06/28 45:56:67
1 Like

Thats working. Not sure what mistake was i doing.
Anyway, thanks a lot.