0

I am trying to use sed in a bash file to add the following after it finds AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

To simplify things I am focusing just on adding the first line until I get it right, I have this so far.....

sed '/AddDefaultCharset UTF-8/a <IfModule mime_magic_module>' /home/testfile.ini

But when I try running this it just echos out the entire file, where am I going wrong?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
fightstarr20
  • 237
  • 1
  • 2
  • 8
  • 5
    Does this answer your question? [How can I get my sed command to make permanent changes to a file?](https://unix.stackexchange.com/questions/95884/how-can-i-get-my-sed-command-to-make-permanent-changes-to-a-file) – Quasímodo Jan 03 '21 at 21:24
  • 2
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Jan 03 '21 at 21:26
  • 2
    @Cyrus Apache configuration files might look like XML, but they aren't. – Freddy Jan 03 '21 at 21:30
  • Consider `augtool` for editing config files. – ctrl-alt-delor Jan 03 '21 at 21:45
  • 1
    Sounds like `AddDefaultCharset UTF-8` doesn't exist in your file. Maybe the space is a tab instead of a blank, idk. Try just `sed -n '/AddDefaultCharset UTF-8/p'` and/or searching for smaller substrings of that (e.g. `AddDefault`, etc.) instead til you figure out where you're going wrong with your search regexp. – Ed Morton Jan 03 '21 at 22:06

1 Answers1

1

Use -e before your sed command(s).

sed -e '/AddDefaultCharset UTF-8/a <IfModule mime_magic_module>\n    MIMEMagicFile conf/magic\n</IfModule>' your_conf_file.ini

NOTE: You can use -i.bak to perform inplace edit and create a backup with .bak (or any other suffix).

benaja
  • 52
  • 3