linux中grep基本用法

215 阅读1分钟
原文链接: click.aliyun.com

grep 查找并且提取文件内容
常用的匹配模式
hello 包含字符hello
^hello 以字符串hello开头
hello$ 以字符串hello结尾

语法格式
grep "条件" 文件名称

例:
1.在/hello/a.txt文件中找出包含 hello 的行

grep "hello" /hello/a.txt

hello
11 hello2

2.在/hello/a.txt文件中找出以 hello开头的行

grep "^hello" /hello/a.txt

hello

3.在/hello/a.txt文件中找出以hello结尾的行

# grep "hello$" /hello/a.txt
hello

4.在/hello/a.txt文件中找出以hello开头的行,并且写入到b.txt文件中

grep "^hello" /hello/a.txt > b.txt

[root@hello hello]# cat b.txt
hello