sed sed sed less sed's please

David Bandel david.bandel
Tue Nov 2 06:57:23 PST 2004


On Tue, 2 Nov 2004 16:09:48 +1100, James McDonald
<james at jamesmcdonald.id.au> wrote:
>  
> I am trying to do a multiple find and replace in a text file I have the
> following code... which is a series of piped sed's 
>   
> can anyone show me a better way I'm hoping there is a way to do the whole 3
> replacements with one call to sed? 
>   
> $ cat test.fw | sed -e 's/INPUT/CUSTOMINPUT/g' \ 
> | sed -e 's/OUTPUT/CUSTOMOUTPUT/g' \ 
> | sed -e 's/FORWARD/CUSTOMFORWARD/g' > test2.fw 

You have two options in this case:
1.  change to a multiple sed syntax:
     a.  put -e before each instruction:
           sed -e 's/OUTPUT/CUSTOMOUTPUT/g' -e 's/INPUT/CUSTOMINPUT/g'
test.fw > test2.fw
     b. use semicolons between instructions:
           sed -e 's/OUTPUT/CUSTOMOUTPUT/g ; s/INPUT/CUSTOMINPUT/g'
test.fw > test2.fw
(note how and where to use the ' to surround sed instructions; also
note I didn't use cat and a pipe).

2.  use a sed script:
---begin script---
     s/INPUT/CUSTOMINPUT/g
     s/OUTPUT/CUSTOMOUTPUT/g
---end script--
    sed -f sedscript test.fw > test2.fw

hth,

David A. Bandel
-- 
Focus on the dream, not the competition.
            - Nemesis Air Racing Team motto


More information about the Linux-users mailing list