
我怎樣才能替換一個實例
id="ogwb"
和
id="ogwb" name="ogwb" value="<?php echo htmlspecialchars($_POST['ogwb']); ?>"
使用sed
還是替代?
答案1
正確的命令:
sed -E 's/^id="([^"]+)"$/& name="\1" value="<?php echo htmlspecialchars($_POST['"'\1'"']); ?>"/'
請注意,當需要嵌入單引號時,這會從雙引號切換到單引號。在 Bash 中,您不能在單引號內嵌入單引號。時期。
但這並不重要,因為引號不是字串終結者在 Bash 中,它們只是不同引用風格之間的分隔符號。所以:
some-command 'This is in a single quoted string'"'"'This is another quoted string, but is part of the same argument to some-command'
^^^
This single quote is inside of double quotes.
如果您不想擔心 shell 引用如何與 交互sed
,請將sed
命令貼在檔案中並使用 呼叫它sed -f
。
答案2
echo 'id="ogwb"' | sed 's,id=\(.*\),id=\1 name=\1 value="<?php echo htmlspecialchars($_POST[\1]); ?>",'