small sed script on command line.

Can anyone help me get this small sed script to work in shell on the command line?
I need it in a one liner really as i want to edit many scripts in a for loop and dont want to have to invoke a separate script each time.

 #!/bin/sh 

sed '/mailx\ -s.*$ { 
i\ 
#Comment above mailx line 
c\ 
mailx -s "blahblah blah my new subject" $someone_who_cares 
}' $1 

The script needs to match a pattern, insert a line above it and then change the pattern to something else.

I've tried the following and no joy

sed '/mailx\ -s.*$/ { i\ #Comment above mailx line c\ mailx -s "blahblah blah my new subject" $someone_who_cares }' file > file2  

sed '/mailx\ -s.*$/ {; i\; #Comment above mailx line ;c\; mailx -s "blahblah blah my new subject" $someone_who_cares ;};' file > file2

Not sure if I understood; nevertheless:

$> cat infile
eins
zwei
pattern
vier
fuenf
$>sed -e '/pattern/i\#comment' -e 's/pattern/newpattern/' infile
eins
zwei
#comment
newpattern
vier
fuenf

Dude thats great. thanks. I never thought of splitting it into multiple -e 's.

Danke :slight_smile:

---------- Post updated at 11:59 AM ---------- Previous update was at 11:51 AM ----------

Actually bummer its not working. I don't think it likes the i\ on command line which doesnt relaly make sense.

I'm running sed on Solaris 9.

root@host# sed -e '/mailx\ -s.*$/i\#Comment' file >file2
sed: command garbled: /mailx\ -s.*$/i\#comment

i am reading the book <sed&awk>
here is a copy:
The append (a), insert (i), and change (c) commands provide editing functions that are commonly performed with an interactive editor, such as vi. You may find it strange to use these same commands to "enter" text using a noninteractive editor. The syntax of these commands is unusual for sed because they must be specified over multiple lines.

No idea if you have GNU sed (gsed?) on Solaris. You could also do following:

sed '
     /pattern/i\#comment
     s/pattern/newpattern/
' infile

Or as oneliner press CTRL+v CTRL+j to get a newline (represented in the code with that ^J):

sed '/pattern/i\#comment^Js/pattern/newpattern/' infile

Thanks guys. Looks like i'll have to do it in a script then. No biggy i suppose.

I aint got gnused on this box.

Cheers for the info.