常用选项
-E :开启扩展(Extend)的正则表达式。
-i :忽略大小写(ignore case)。
-v :反过来(invert), 显示不包含匹配文本的所有行。
-V 或 --version : 显示版本信息
-n :显示行号
-w :被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker
-c :显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到。
-o :只显示被模式匹配到的字符串。
-r :递归
-f<规则文件> 或 --file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式
--color :将匹配到的内容以颜色高亮显示。
-A n:显示匹配到的字符串所在的行及其后n行,after
-B n:显示匹配到的字符串所在的行及其前n行,before
-C n:显示匹配到的字符串所在的行及其前后各n行,context
[kumufengchun@localhost ~]$ cat sed.txt my cat's name is betty This is your This dog my dog's name is This frank This is your fish my fish's name is This george This is your goat my goat's name is This adam [kumufengchun@localhost ~]$ cat sed.txt | grep -i this This is your This dog my dog's name is This frank This is your fish my fish's name is This george This is your goat my goat's name is This adam [kumufengchun@localhost ~]$ cat sed.txt | grep -v this my cat's name is betty This is your This dog my dog's name is This frank This is your fish my fish's name is This george This is your goat my goat's name is This adam [kumufengchun@localhost ~]$ cat sed.txt | grep -iv this my cat's name is betty [kumufengchun@localhost ~]$ cat sed.txt | grep -n This 3:This is your This dog 4:my dog's name is This frank 6:This is your fish 7:my fish's name is This george 8:This is your goat 9:my goat's name is This adam [kumufengchun@localhost ~]$ cat sed.txt | grep -w This This is your This dog my dog's name is This frank This is your fish my fish's name is This george This is your goat my goat's name is This adam [kumufengchun@localhost ~]$ cat sed.txt | grep -c This 6 [kumufengchun@localhost ~]$ cat sed.txt | grep -o This This This This This This This This [kumufengchun@localhost ~]$ cat sed.txt | grep -A 2 frank my dog's name is This frank This is your fish [kumufengchun@localhost ~]$ cat sed.txt | grep -B 2 frank This is your This dog my dog's name is This frank [kumufengchun@localhost ~]$ cat sed.txt | grep -C 2 frank This is your This dog my dog's name is This frank This is your fish
或的关系 [kumufengchun@localhost ~]$ cat sed.txt | grep -E "frank|adam" my dog's name is This frank my goat's name is This adam
通用类
[[:upper:]] [A-Z]
[[:lower:]] [a-z]
[[:digit:]] [0-9]
[[:alnum:]] [0-9a-zA-Z]
[[:space:]] 空格或tab
[[:alpha:]] [a-zA-Z]
[[:punct:]]标点符号
替换每行的第二个This为小写this [kumufengchun@localhost ~]$ sed -i 's/This/this/2g' sed.txt [kumufengchun@localhost ~]$ grep '[[:lower:]]his' sed.txt This is your this dog [kumufengchun@localhost ~]$ grep '[[:upper:]]his' sed.txt This is your this dog my dog's name is This frank This is your fish my fish's name is This george This is your goat my goat's name is This adam 在文件后边追加 $hello=3 [kumufengchun@localhost ~]$ sed -i '$a$hello=3' sed.txt [kumufengchun@localhost ~]$ cat sed.txt my cat's name is betty This is your this dog my dog's name is This frank This is your fish my fish's name is This george This is your goat my goat's name is This adam $hello=3 [kumufengchun@localhost ~]$ grep "hello[^[:upper:]][[:digit:]]" sed.txt $hello=3 [kumufengchun@localhost ~]$ grep "hello[[:punct:]]" sed.txt $hello=3 [kumufengchun@localhost ~]$ grep "he[[:alnum:]]" sed.txt $hello=3 [kumufengchun@localhost ~]$ grep "hello[[:punct:]]" sed.txt $hello=3