3

I've written a sed interpreter script:

#!/usr/bin/sed -E -f
s/ +/_/g
s/[.':]//g
s/&/and/g

However, when I run it:

$ echo "Bob Dylan" | shscrub.sed
sed: unknown option --  
usage: sed [-aEnru] command [file ...]
       sed [-aEnru] [-e command] [-f command_file] [file ...]

I need the -E option because I'm using the Extended Regular Expression syntax '+'.

Any ideas what I'm doing wrong?

Edit

A workaround:

#!/usr/bin/sed -f
s/ \{1,\}/_/g
s/[.':]//g
s/&/and/g

But, I'd still like to know how I can pass two parameters in the shebang (#!) line.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Joe Snikeris
  • 1,150
  • 12
  • 17
  • What version of sed are you using? – Steve Dec 31 '10 at 16:32
  • 1
    The default sed on OpenBSD 4.8. Says: The sed utility is compliant with the IEEE Std 1003.1-2008 (``POSIX'') specification. The flags [-aEru] are extensions to that specification. – Joe Snikeris Dec 31 '10 at 16:43

3 Answers3

3

The error message was saying that the space character isn't an option. So I guess, you need to mash all the arguments together in the shebang:

#!/usr/bin/sed -Ef
s/ +/_/g
s/[.':]//g
s/&/and/g

Strange, because this works fine:

$ echo "Bob  Dylan" | sed -E -f ~/bin/shscrub.sed
Bob_Dylan
Joe Snikeris
  • 1,150
  • 12
  • 17
  • Looks like a Gnu sed bug to me. – ismail Dec 31 '10 at 16:59
  • 5
    +1 Shebang lines usually can only have one space after the name of the interpreter. That's why you have to run the arguments together even when you can separate them on the command line. – Dennis Williamson Dec 31 '10 at 18:36
  • 1
    Shebang lines have no environment: `$IFS` isn't set, so the shell doesn't know what characters to use to separate options. You could probably work around this by transferring an environment, perhaps with `/usr/bin/env`. – Zenexer May 23 '13 at 04:26
2

The problem is the shebang #! multiple CLI arguments which get treated as a single '-E -f' argument by the Linux kernel through binfmt_misc: https://stackoverflow.com/a/1667855/895245

As mentioned in: How to use multiple arguments with a shebang (i.e. #!)? there seems to be no nice way to get around that except with a wrapper.

And since you mentioned POSIX, also note that there here is no mention of -E in POSIX 7: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
1

Works with the default sed on OSX 10.6;

[~]> echo "Bob Dylan"|./test.sed
Bob_Dylan

Using plain sh works too;

[~]> sh -c "echo \"Bob Dylan\" | ./test.sed" 
Bob_Dylan

Update: Doesn't work with Gnu sed indeed, seems to be a compatibility problem.

With GNU sed it works if you don't use the -f parameter, note that you have to use -r instead of -E;

[~]> echo "Bob Dylan"|sed -r "s/ +/_/g;s/[.':]//g;s/&/and/g" 
Bob_Dylan
ismail
  • 46,010
  • 9
  • 86
  • 95