grep 用法实例 linux

打印出文本的前20行内容
[root@zhangshibo temp]# cat actress_rankings.txt | head -n 20
1 Keira Knightley
2 Natalie Portman
3 Monica Bellucci
4 Bonnie Hunt
5 Cameron Diaz
6 Annie Potts
7 Liv Tyler
8 Julie Andrews
9 Lindsay Lohan
10 Catherine Zeta-Jones
11 CateBlanchett
12 Sarah Michelle Gellar
13 Carrie Fisher
14 Shannon Elizabeth
15 Julia Roberts
16 Sally Field
17 TeaLeoni
18 Kirsten Dunst
19 Rene Russo
20 JadaPinkett

 

打印出匹配 “Cameron Diaz  的文本行以及其之后的3行
[root@zhangshibo temp]# grep -A 3 "Cameron Diaz" actress_rankings.txt
5 Cameron Diaz
6 Annie Potts
7 Liv Tyler
8 Julie Andrews

 

打印出匹配行及其之前的3行
[root@zhangshibo temp]# grep -B 3 "Cameron Diaz" actress_rankings.txt
2 Natalie Portman
3 Monica Bellucci
4 Bonnie Hunt
5 Cameron Diaz

打印出匹配行及其之前的2行和之后的2行
[root@zhangshibo temp]# grep -C 3 "Cameron Diaz" actress_rankings.txt
 2 Natalie Portman
3 Monica Bellucci
4 Bonnie Hunt
5 Cameron Diaz
6 Annie Potts
7 Liv Tyler
8 Julie Andrews

你可能感兴趣的:(grep 用法实例 linux)