$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