0

I want to run a script that changes a line in the HTML code, indicating when the page was last updated. So for instance, I have the line

<d>This page was last updated on 29.04.2013 at 00:34 UTC</d>

and I am updating it now, so I want to replace that line with

<d>This page was last updated on 15.05.2013 at 15:50 UTC</d>

This is the only line in my source code that has the <d> tag, so hopefully that helps. I already have some code that generates the new string with the current date and time, but I can't figure out a way to replace the old one (which changes, so I don't know exactly what it is).

I've tried putting in a comment <!--date--> in the previous line, deleting the whole line that has <d> (with grep), and then putting in a new line after the comment that is the new string, but that fails. For example, if I want to just insert the string text after the comment, and use

sed -i 's/<!--date-->/<!--date-->text/' file.html

I get invalid command code j. I think it might be because there are some special characters like <,!, and > in the strings, but if I want to put in the date string above, I will have even more, like : and /. Thanks for any ideas on how to fix this.

Jānis Lazovskis
  • 190
  • 2
  • 13
  • Believe it or not, I think the problem is that you are using `csh` or some other shell that is trying to do a history expansion on the `!` inside single quotes. Try using a different shell. – William Pursell May 15 '13 at 15:00

3 Answers3

1

You don't need your <!--date--> hack. You can use regular expressions and another delimiter besides "/" in your sed command:

sed -i.bak 's@<d>This page was last updated on.*</d>@<d>This page was last updated on 12.05.2013 at 00:38 UTC</d>@' whatever.html

Or, if you have your update in a variable called $replacement:

sed -i.bak "s@<d>This page was last updated on.*</d>@$replacement@" whatever.html
Markku K.
  • 3,840
  • 19
  • 20
  • Nope, in both cases, using `bash` or `sh`, I get the same error. I should mention I'm running Mac OS 10.7.5 – Jānis Lazovskis May 15 '13 at 15:11
  • Hmm ... works in bash, csh, and zsh on my Linux box ... sorry then, I don't know! – Markku K. May 15 '13 at 15:20
  • OK, see [this](https://github.com/bergie/create/pull/119) ... you need a "backup" extension on your -i ... -i.bak or whatever. – Markku K. May 15 '13 at 15:22
  • Great, now it works! But there should be no space between `-i` and `.bak`, otherwise it fails. – Jānis Lazovskis May 15 '13 at 15:32
  • That's funny ... I originally had it without the space, but I saw [this answer](http://stackoverflow.com/questions/4247068/sed-command-failing-on-mac-but-works-on-linux) which had a space. – Markku K. May 15 '13 at 15:57
1

This will change the text only on lines that contain <d>:

sed -i.bak "/<d>/s/on .* at [^<]*/on newdate at newtime/" file.html

I've tested this with the BSD sed that ships with MacOS X 10.8.3

chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
0

When using the command line, try escaping special characters like this:
! ===> \!

yoones
  • 2,394
  • 1
  • 16
  • 20