17

私は sed スクリプト ファイルに取り組んでいますが、実行すると「Invalid Preceding regular expression」エラーが発生します。以下はファイル全体です。

このサイトと他の場所の両方で、これについてすでに多くの検索を行っています。ここで尋ねられた多くの質問は、正規表現を拡張する必要があるという結果になりました。何かが間違ってエスケープされています。これは、電子メールの置換に必要なため、すでに拡張表現として定義しています。

#!/bin/sed -rf
#/find_this thing/{
#s/ search_for_this/ replace_with_this/
#s/ search_for_this_other_thing/ replace_with_this_other_thing/
#}

#Search and replace #ServerAdmin (with preceding no space) email addresses using a regular expression that has the .com .net and so on domain endings as option so it will find root@localhost and replace it in line with admin's email address.

ServerAdmin/ { 
s/\b[A-Za-z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+(\.[A-Za-z]]{2,4})?\b/email@example.com/
}
#Enable user's Public HTML directories
/UserDir/ {
s/disable$/enable/ 
s/^#User/User/
}
#Replace the only #ServerName (with preceding no space) followed space and text with Our server ip
/#ServerName */ c\ ServerName server.ip.address.here/

ターミナルから ./config-apache.sed /etc/httpd/conf/httpd.conf として呼び出しており、これが返されます。

/bin/sed: file ./apache-install.sed line 12: Invalid preceding regular expression

vim行12の内部は、}上記のシングルとして識別されます#Enable user's Public HTML directories

4

1 に答える 1

24

GNUsedは、PCRE の非キャプチャー表記を好まないようです。

...(?:...)...

試す:

s/\b[A-Za-z0-9._%-]+@([a-zA-Z0-9-]+\.)+(\.[A-Za-z]]{2,4})?\b/email@example.com/

GNUsedはそれでOKのようです。ただし、まだ少し作業があります。以下の最初の行を入力とすると、出力は 2 行目になります。

abc def@ghi.jk aaa
abc email@example.comjk aaa

その結果を与える 2 つの問題があります。

  1. ]]は単一のである必要があり]ます。
  2. 前の正規表現で末尾のドットを探しているので、ドメイン サフィックスの最後の部分には必要ありません。

これは仕事をします:

s/\b[A-Za-z0-9._%-]+@([a-zA-Z0-9-]+\.)+([A-Za-z]{2,4})?\b/email@example.com/

abc def@ghi.jk aaa
abc email@example.com aaa
于 2013-02-18T04:58:50.617 に答える