开发者

How to use Posix Extended Regular Expressions in the shebang of sed interpreter script?

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: unkno开发者_如何学Gown 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.


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜