How to mass Search & Replace in text files.

D. Hugh Redelmeier hugh-pmF8o41NoarQT0dZR+AlfA at public.gmane.org
Fri May 1 15:22:14 UTC 2009


| From: Lance F. Squire <lance-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org>

| sed 's/Bad JS//' *html
| 
| Would that search recursively?

Classically, sed's output goes to stdout.  Some time between when I
learned sed (1970's) and now, someone added the -i flag to allow
in-place editing, and you would need that.

Your question about recursive searching shows that you haven't got a
good internal model about how the shell ecosystem works.

If you want recursion, you generally have to use find(1).  The latest
BASH (which you probably don't have) adds ** for this purpose.

Here's a guess.  Untested.  Any bug might be very destructive.

  find /root/of/tree/with/html -name '*.html' -print0 \
  | xargs --no-run-if-empty --null sed -i -e 's/BadJS//g'

Notes:

- This will rewrite files even if there is no change.  You may not
  want that.

- the -print0 and --null prevent odd filenames confusing the script.
  For example, filenames with spaces in them.

- the "g" at the end means that more than one substitution per line is
  attempted.

- are you sure that this is all the damage that needs fixing?
  Proving that would seem tough and a mistake might be bad.

- in-place changes are a bit like burning bridges after you cross
  them.  If you make a mistake, you cannot go back.
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list