sed error

This is driving me nuts - especially since it worked as an alias. I've tried to simplify the command in .bashrc :

81 function phpfind {
82         #find $MROOT -name "*.php" | sed "s/\/var\/www\/\/docs\/moodle/\$M/" | xargs egrep
83         find $MROOT -name "*.php" | sed -e "s/v/x/"
84 }

but this is the error i'm getting. Why?

sed: -e expression #1, char 46: unknown option to `s'

LVL 86
Who is Participating?
Duncan RoeSoftware DeveloperCommented:
Use another separator than "/" in the sed command. Did you miss a backslash after "moodle" by any chance? Also shell strips a level of backslashes before sed gets to see the command line - that's the difference between an alias and a shell script BTW. Usually yoe see sed's -e program in single quotes to avoid that, but of course you want parameter substitution. Maybe:

  find $MROOT -name "*.php" | sed "s%/var/www/$M%" | xargs egrep
CEHJAuthor Commented:
Actually i'm beginning to confuse myself - i think i;m getting tired. What i really intended do was to run that send command if the argument of -l goes to sed, so i suppose it should come after egrep and before the last argument to egrep, but only if i wanted an -l type result. Ugh. Better come back to this tomorrow maybe ;-)
Line 83 doesn't have 46 characters (char 46) in the sed expression, only 6 chars, so it must refer to the one in line 82.

In line 82, the 46th character the error is referring to is the final forward slash, right before the closing double quote.  In otherwords, there is a total of 46 characters in the whole sed expression in line 82, which means the shell script is not stripping the backslashes between the double quotes.  About the only thing the shell does inside double quotes is parameter expansion.  The other exception is to replace octal (\###) or hex (\x##) numbers with their ascii equivalent, and to allow special escape sequences, such as \b for backspace, \n for newline, \t for a tab, etc..  Almost all other characters are left alone and do nothing special (even single quotes inside double quotes are only single quotes)........

But, as duncan_roe points out, use a different character instead of a forward slash right after the 's' at the start of the expression......It's much less confusing that way, and easier to read........It also might be the cause of the error, in a roundabout way.......Although the forward slashes in the path name have all been escaped from your example above, it might have something to do with the parameter expansion with "$M", such as a forward slash in the variable's replacement value (ie., see if there is a forward slash in the variable's replacement value)..........

Also, another tip is to escape the dot with a backslash in the regexp part of the expression (proteanit\.net).........In this particular case it might not make a difference, but a single dot in a regexp is equivalent to the '?' wildcard in a filename expansion, which is to replace it with any single character.........It's just a good habit to get into, whether or not it makes a difference in the expression.......However, the backslash is not required in the substition part of the expression for the '$M' (as in duncan_roe's example)......

One more thing, are you sure the expression in the phpfind function is exactly the same as what was in the alias?.......I'm sure you already doubled-checked that, but I had to ask anyway.... :-)

CEHJAuthor Commented:
Great comments folks. I meant to say

'What i really intended do was to run that sed command if the argument of -l goes to sed'

so i'm going to have to rethink this
CEHJAuthor Commented:
Thanks for the comments
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.