
我正在尋找一個結合這兩個命令的優雅解決方案:
sed -i '/Y/! s/replace/with/' /path/to/file
sed -i '/X/ s/replace/with/' /path/to/file
我試過
sed -i '/X/ /Y/! s/replace/with/' /path/to/file
這是行不通的。有優雅的解決方案嗎?
答案1
也許是這樣sed '/X/ {/Y/! s/replace/with/}'
?
$ sed '/X/ {/Y/! s/replace/with/}' << EOF
X replace X
X replace Y
Y replace X
Y replace Y
EOF
X with X
X replace Y
Y replace X
Y replace Y