Re: [SLUG] Need help with a sed script

From: Eben King (eben1@tampabay.rr.com)
Date: Sat Aug 02 2003 - 13:09:57 EDT


On Sat, 2 Aug 2003, Russ Wright wrote:

> I have a simple script to perform a find and replace for me. Currently it
> looks for "<?" and replaces it with "<?php".
>
> for fl in *.php; do
> mv $fl $fl.old
> sed 's/<? /<?php /g' $fl.old > $fl

Are you guaranteed to have only one <? per line? That only finds the
first instance on any given line, then goes to the next line. If there
might be more than one, you'd have to either do sed ...g or sed 's/</
</g' or something.

> rm -f $fl.old
> done
>
> Works pretty well.

Anyhow, something like

. disclaimers/standard

for fl in *.php; do
  mv $fl $fl.old || exit
  sed -e '/<?php/q' \
      -e 's/<?=/<php/' \
      -e '/<?$/b php' \
      -e 'p;q;' \
      -e ': php' \
      -e N \
      -e 's/<?\n/<?php/' $fl.old > $fl || exit
  rm -f $fl.old
done

That's an untested first draft. A solution that puts the iteration inside
sed would be better, as this calls sed for each file. There may be bugs.
Somebody else posted a perl solution; that may be faster. Of course, if
you don't speak perl, there goes maintainability.

-- 
-eben    ebQenW1@EtaRmpTabYayU.rIr.OcoPm    home.tampabay.rr.com/hactar
SAGITTARIUS:  All your friends are laughing behind your back... kill
them.  Take down all those naked pictures of Ernest Borgnine you've got
hanging in your den.  -- Weird Al, _Your Horoscope for Today_

----------------------------------------------------------------------- This list is provided as an unmoderated internet service by Networked Knowledge Systems (NKS). Views and opinions expressed in messages posted are those of the author and do not necessarily reflect the official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 15:55:50 EDT