$echo "This is a test " | sed -n '/test/p' This is a test #純文本適配對大小寫敏感 $echo "This is a test " | sed -n '/this/p' $ $echo "This is a test " | sed -n '/This/p' This is a test #不須要單詞所有拼完,只要有就能夠匹配 #在正則表達式中可使用空格和數字,並且空格和其餘字符串並無什麼區別 $echo "This is number 1" | sed -n '/ber1/p' $ $echo "This is number 1" | sed -n '/ber 1/p' This is number 1 #單詞間有兩個空格的行匹配正則表達式模式,是找出多餘空格的好方法
# ^ 表明了行首 $echo "this is a test" | sed -n '/^this/p' $this is a test #只要單詞是在行首,就能匹配到 # $表明了行尾 $echo "this is a test" | sed -n '/test$/p' $this is a test #只要單詞是在行尾,就能匹配到 # ^$ 表示空白行,利用sed的/^$/d,能夠刪除文件中的空白行
#點字符能夠匹配除換行符以外的任何 單個 字符。 $ echo "this is a nice cat" | sed -n '/.at/p' $this is a nice cat $ echo "this is a nice cat" | sed -n '/.th/p' $ #由於th前沒東西,因此沒法匹配
$echo "The cat is sleeping" | sed -n '/[ch]at/p' $ The cat is sleeping $echo "The hat is nice" | sed -n '/[ch]at/p' $The hat is nice # #單個表達式中可使用多個字符組 $echo "yes" | sed -n '/[Yy]e[Ss]/p' $yes
$echo "it is three o'clock" | sed -n '/[^at]hree/p' $ #由於three已經被排除了
$echo "ik" | sed -n '/ie*k/p' $ik
其實就是對基礎表達式中星號的細化正則表達式
$echo "The cat is sleeping" | gawk '/cat|dog/{print $0}' $The cat is sleeping $echo "The dog is sleeping" | gawk '/cat|dog/{print $0}' $The dog is sleeping