我想用sed
中的正則表達式替換字符串。現在,我知道sed
的行爲在Mac上很時髦。我經常看到使用egrep
的解決方法,當我想檢查一行中的某個模式時。但是,在這種情況下,我想做字符串替換。如何讓sed獲取擴展正則表達式?
我想用gggg
代替cp an
和cp <tab or newline> an
。我嘗試了以下,這將在擴展的正則表達式下工作:
sed -i'_backup' 's/cp\s+an/gggg/g'
但是,當然這沒有什麼。我嘗試了egrep
ping,當然它用cp <one or more space characters> an
挑出了一行。
如何獲得sed
使用擴展正則表達式進行替換?或者什麼是使用正則表達式進行替換的更好方法?
我在mac osx上。
安裝GNU coreutils可讓您訪問GNU sed。 'brew install coreutils'如果你已經有了Homebrew的話。 –
這個Q可能更適合於相關網站http://apple.stackexchange.com(AskDifferent)。考慮使用Q底部的'flag'鏈接並請主持人將其移動到那裏。祝你好運。 – shellter
[sed'的手冊頁](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sed.1.html)在一段標題爲「Sed Regular表達式」。它鏈接到[re_format手冊頁](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man7/re_format.7.html),它解釋了「現代」(又名「擴展」)正則表達式。 –