0

私は Zend CRUD ジェネレーターで作業しており、$targetForm ファイルにある「test」という単語を各フィールドのフォーム コードに置き換える必要があります。

field[0]="foo"
field[1]="bar"

textareafield='$'"acme_en = new Utils_Form_Element_Textarea('acme_en',array('langblock'=>'en', 'isWysiwyg' => true));
    "'$'"this->addElement("'$'"acme_en);
    "'$'"this->addElement('textarea','acme_fr', array( 'label'=>__('acme'), 'langblock'=>'fr', 'isWysiwyg' => true, 'altLangElem' => "'$'"acme_en));"    

for ((i=0; i<${#field[@]}; i++));
do
    formfield[$i]=$textareafield
    formfield[$i]=${formfield[$i]//acme/${field[$i]}}
    echo ${formfield[$i]}
    sed -i "s/test/test\n        ${formfield[$i]}/" $targetForm
done

コマンドラインには次のように書かれています:

$foo_en = new Utils_Form_Element_Textarea('foo_en', array('langblock'=>'en', 'isWysiwyg' => true)); $this->addElement($foo_en); $this->addElement('textarea','foo_fr', array( 'label'=>__('foo'), 'langblock'=>'fr', 'isWysiwyg' => true, 'altLangElem' => $foo_en));
sed: -e expression #1, char 120: unterminated `s' command
$bar_en = new Utils_Form_Element_Textarea('bar_en', array('langblock'=>'en', 'isWysiwyg' => true)); $this->addElement($bar_en); $this->addElement('textarea','bar_fr', array( 'label'=>__('bar'), 'langblock'=>'fr', 'isWysiwyg' => true, 'altLangElem' => $bar_en));
sed: -e expression #1, char 120: unterminated `s' command

特殊文字に問題があるのか​​もしれませんが、解決方法がわかりません。

4

2 に答える 2

2

This error you get very likely has nothing to do with using \n in sed substitution (especially since you mentioned in the comments that you are using GNU sed version 4.2.1).


The real culprit is textareafield which contains a multi-line string.

What happens is that when ${formfield[$i]} gets expanded, your sed command looks like this

sed -i "s/test/test\n        line1
line2
line3/"

This is problematic because without terminating lines with a literal \, sed would interpret each line as a complete command in itself, in this case

s/test/test\n        line1

which is missing a / at the end, hence the error "unterminated `s' command".

To fix this, what we want is to insert a \ to the end of every line except the last, i.e.

sed -i "s/test/test\n        line1\
line2\
line3/"

I got your example to work by adding two lines

for ((i=0; i<${#field[@]}; i++));
do
    formfield[$i]=$textareafield
    formfield[$i]=${formfield[$i]//acme/${field[$i]}}
    formfield[$i]=${formfield[$i]//\;/\;\\}
    formfield[$i]=${formfield[$i]%\\}
    echo ${formfield[$i]}
    sed -i "s/test/test\n        ${formfield[$i]}/" $targetForm
done

Since all the lines in textareafield end in ;, I replaced all ; with ;\, then removed the last \ from the last line.

于 2012-11-13T10:36:58.067 に答える
1

sed \nは置換をサポートしていませんが、エスケープすればリテラルの改行を使用できます。また、'文字を引用する必要があります。これを行う1つの方法は次のとおりです。

for ((i=0; i<${#field[@]}; i++));
do
    formfield[$i]=$textareafield
    formfield[$i]=${formfield[$i]//acme/${field[$i]}}
    sedexpr="${formfield[$i]}"
    sedexpr="${sedexpr//\'/\'}"
    sedexpr="${sedexpr//
/\\
}"
    sed -i "s/test/test\
        ${sedexpr}/" $targetForm
done

しかし、このレベルの複雑さでは、おそらく Python のようなものに切り替えたほうがよいでしょう。

于 2012-11-12T22:23:09.487 に答える