Linux grep 命令实例

# vim test2.txt
Christian Scott lives here and will put on a Christmas party.
There are around 30 to 35 people invited.
They are:
 Tom
Dan
Rhonda Savage
Nicky and Kimerly.
Steve, Suzanne, Ginger and Larry.

大写字母开头,总长度为3位。

# cat test2.txt | grep '^[A-Z]..$'
Dan

空格开头,第一个字母大写字母开头,总长度为3位。

# cat test2.txt | grep '^ [A-Z]..$'
Tom

大写字母开头,包含数字:

# cat test2.txt | grep '^[A-Z]*.*[0-9]'
There are around 30 to 35 people invited.

大小写字母开头,包含逗号:

# cat test2.txt | grep '^[A-Za-z]*.*[,][A-Za-z]*.*$'
Steve, Suzanne, Ginger and Larry.

目录中以z开头的文件:

# ls -l /bin/ | grep '[0-9] z[a-z]*.*$'
# ls /bin/ | grep '^z'

目录中不包含大写字母A-Y,小写字母a-Y及数字0-9的文件:

# ls /bin/ | grep '^[^A-Ya-y0-9]'

目录中以x或者z开头的文件:

# ls /bin/ | grep '^[xz]'

目录中包含8或者6的文件:

# ls /bin/ | grep '[86]'

目录中以gc开头,长度为3个字符的文件:

# ls /bin/bash | grep '^gc.$'
gcc

目录中以op结尾,长度为4个字符的文件  

# ls /bin/bash | grep '^..op$'
htop

目录中以z开头,h结尾,长度为3个字符的文件:

# ls /bin/bash | grep '^z.h$'
zsh

目录中以t开头,e或者p结尾,长度为3个字符的文件:  

# ls /bin/bash | grep '^to[ep]'
toe
top

目录中以t开头,以数字6结尾的文件:

# ls | grep '^t[a-z]*6$'
tracepath6
traceroute6

 

[THE END] 

posted @ 2021-07-05 18:50  Lambeto  阅读(150)  评论(0编辑  收藏  举报