-2

I want to replace <title> with <title>FRED, using sed. I have tried the below to no avail.

19:46: scratch $ sed -i '/<title>/<title>FRED/' index.html
sed: -e expression #1, char 10: unknown command: `<'
======================================================================================================================
19:47: scratch $ sed -i '/\<title>/<title>FRED/' index.html
sed: -e expression #1, char 11: unknown command: `<'
======================================================================================================================
19:48: scratch $ sed -i "/\<title>/<title>FRED/" index.html
sed: -e expression #1, char 11: unknown command: `<'

How then, do I escape the characters < and > correctly?

Or is it something else I'm not getting?

teraspora
  • 415
  • 4
  • 17
  • 1
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Nov 24 '19 at 20:02
  • What's wrong with `sed`? I just want, in a script, to add some text to a file. And the text I'm searching for contains `<` and `>`. Let's forget it's HTML please! What should I use if not `sed`? – teraspora Nov 24 '19 at 20:12
  • `sed` is fine for editing *unstructured* text. Regular expressions aren't powerful enough to correctly parse HTML, at least not without knowing very specifically how that HTML is formatted. – chepner Nov 24 '19 at 20:13
  • I don't need to "parse HTML". Let's say I just need to find text in a string that has `<` in it! I thought I could escape special characters with a `\\`backslash. – teraspora Nov 24 '19 at 20:15
  • Some variation using back reference `sed -ri 's/()/\1FRED/' file` or `sed -i 's/\(<title>\)/\1FRED/' file` – Jotne Nov 25 '19 at 06:49

1 Answers1

4

You haven't specified a command. You presumably wanted the substitution command

sed -i 's/<title>/<title>FRED/' index.html

Without the s, sed thinks that /<title>/ is an address for the command < (which doesn't exist in sed).

chepner
  • 497,756
  • 71
  • 530
  • 681