3

I have one awk and one sed example. Both seems to be using the same regular expression, but the output differs. So the pattern expansion must be getting different. But i can not get the difference. Can anyone explain?

 $ echo "THE BEAR ATE THE FISH" |sed -e 's/[^ ]*(H|F)[^ ]*/<red>&/g'
THE BEAR ATE THE FISH
 $ echo "THE BEAR ATE THE FISH" | awk 'gsub(/[^ ]*(H|F)[^ ]*/,"<red>&")'
<red>THE BEAR ATE <red>THE <red>FISH
 $ sed --version
GNU sed version 4.1.5
 $ awk --version
GNU Awk 3.1.5
4

1 回答 1

8

您在您的正则表达式中使用交替|,这是扩展正则表达式类的一部分。不同之处在于sed (and grep)当你想使用扩展的正则表达式时,你需要通过给出-r标志 withGNU sed-Ewith来明确GNU grep

$ echo "THE BEAR ATE THE FISH" | sed -re 's/[^ ]*(H|F)[^ ]*/<red>&/g'
<red>THE BEAR ATE <red>THE <red>FISH

或者,您可以转义扩展的正则表达式功能:

$ echo "THE BEAR ATE THE FISH" |sed -e 's/[^ ]*\(H\|F\)[^ ]*/<red>&/g'
<red>THE BEAR ATE <red>THE <red>FISH

更好的是,您可以在这里使用字符类:

$ echo "THE BEAR ATE THE FISH" |sed 's/[^ ]*[HF][^ ]*/<red>&/g'
<red>THE BEAR ATE <red>THE <red>FISH

所有这三个工具的手册页都有解释正则表达式使用的部分。

于 2013-04-23T12:56:00.953 回答