String replacement with sed

Giorgos Keramidas keramida at ceid.upatras.gr
Fri Sep 3 16:49:40 PDT 2004


On 2004-09-03 19:37, jd <web at 3dresearch.com> wrote:
>
> I need to change a bunch of Analog config files; among other things
> change the location of IMAGEDIR. I made this simple script:
>
> #!/bin/sh
> for loop in `ls analog/*`
> do
>     sed -e '/IMAGEDIR/s/www2.3dresearch.com\/analog_images\//fiordiligi.3dresearch.com\/images\//p' $loop > $loop.sed
> done
>
> It works fine, except I get duplicate lines, such as:
>
> IMAGEDIR                http://fiordiligi.3dresearch.com/images/
> IMAGEDIR                http://fiordiligi.3dresearch.com/images/

Remove the trailing 'p' from your substitution pattern or use the same
regexp with the -n option of sed, i.e.:

$ echo foo bar | sed -e '/foo/ s/foo/FOO/p'
FOO bar
FOO bar
$ echo foo bar | sed -n -e '/foo/ s/foo/FOO/p'
FOO bar
$ echo foo bar | sed -e '/foo/ s/foo/FOO/'
FOO bar
$



More information about the freebsd-questions mailing list