Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ubuntu 9.10

Hi Guys,

I want to create a script that will automatically update a single line in an .htaccess file. Could someone please point me to some instructions for creating a basic "find and replace" script?

I'm imagining something that basically says "look for special markers #ABC and #XYZ... remove entries between the markers and replace it with 'foo'".

share|improve this question
up vote 7 down vote accepted

You want sed: sed -i s/regex to look for/string to replace it with/

If it needs to be multiple lines long, you can write more complicated scripts, although at some point sed's power and brevity does start to work against it, and I prefer to start writing in a more verbose but generally comprehensible scripting language (I prefer Ruby, but tastes vary).

If you provide an actual example of what you're trying to accomplish, someone can probably come up with "the best" way to do it; at the moment your example is a big vague to give a concrete solution.

share|improve this answer
when using sed, you can also use a sedscript that contains your commands and call it with -f. Man page: – Greeblesnort Dec 3 '09 at 3:08
What's this obsession with linking to random manpage sites on the web instead of just running man sed? Especially when the ones on your system will document whatever you're running, instead of some random version which may or may not be the same or even compatible? – womble Dec 3 '09 at 3:28
is that not safer than assuming everyone is running on a linux box? – Greeblesnort Dec 3 '09 at 20:04
Given that you're linking to a GNU sed man page, and it's pretty rare for installation of GNUtilities not to come with man pages, I'd say yeah, it's not safer. – womble Dec 4 '09 at 23:24

Try this:

awk '/^#ABC/ {start=1; print; next} /^#XYZ/ {start=0; print "foo"; print; next} start!=1 {print}' .htaccess > .htaccess.NEW

It can be shortened to:

awk '/^#ABC/ {start=1; print; next} /^#XYZ/ {start=0; print "foo"} start!=1 {print}' .htaccess > .htaccess.NEW
share|improve this answer

If your files are small (.htaccess should be, methinks), then Perl can "slurp" them up and operate on the whole thing as a single string, using s///s (the 's' modifier at the end treats the whole string as a single line).

Example: perl -i.bak -p -0 -e 's/#ABC\n.*#XYZ/foo/s' list of file names

-i.bak tells perl to save backups, in this case called list.bak of.bak file.bak names.bak -p -0 tells perl to slurp the whole file and -e tells it to apply the regular expression on the command line.

This particular version will only replace the first set of markers, and will do nothing if the markers don't exist at all (but it will still create the backup files).

share|improve this answer
This script also replaces markers, don't know if it is the needed functionality. – Icapan Dec 7 '09 at 14:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.