1829110 Members
12224 Online
109986 Solutions
New Discussion

2 sed in one

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

2 sed in one

if I have 2 sed statements

sed "s/north/south/ " input | sed "s/east/west/" >output

is there any way I can just use one sed?

I mean, is there anything like

sed -e "s/north/south/ " -e "s/east/west/" input >output
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: 2 sed in one

Hi:

Yes, you can stack 'sed' scripts with multiple '-e' scripts.

For example:

# echo "one\ntwo"|sed -e 's/one/ONE/' -e 's/two/TWO/'

Regards!

...JRF...
Mel Burslan
Honored Contributor

Re: 2 sed in one

the last statement you have typed is perfectly allright as far as I can tell.. you just need to give the substitute command range as line numbers I believe as such:

sed -e "1,\$s/north/south/ " -e "1,\$s/east/west/" input >output

to get the whole file processed
________________________________
UNIX because I majored in cryptology...
curt larson_1
Honored Contributor

Re: 2 sed in one

yes it works just like you wrote

or you can use a file

file contanins
s/north/south/
s/east/west

then execute sed -f file input > output
TwoProc
Honored Contributor

Re: 2 sed in one

If it gets complicated you can stack them in a file.

$cat repl.sed
s/north/south/
s/east/west/

sed -f repl.sed input > output

I realize that this is not necessary for only two expressions, so this is just an fyi in case you need.

I use these types in cases where I need a program to generate sed conditions for me dynamically, and then the sed needs be applied to a stream programmatically.

We are the people our parents warned us about --Jimmy Buffett
Gemini_2
Regular Advisor

Re: 2 sed in one

yes, it worked for me now..I think that I miss one "s", that was why it didnt work.

thanks everyone
Muthukumar_5
Honored Contributor

Re: 2 sed in one

Use perl easily instead of sed as,

perl -pe 's/one/ONE/;s/two/TWO/' input > output

If you want to update in the same file then,

perl -pi -e 's/one/ONE/;s/two/TWO/' input-same

hth.
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: 2 sed in one

Hi,
use:

sed "s/north/south/;s/east/west/" input >output

Art