我正在尝试 GREP 使用 Ubuntu 赢得超过 50 场比赛的车手。
这是我正在使用的命令:
cat f1.txt | grep -E " ([5-9][1-9]|[1-9][0-9][0-9])*" | sort -n -k5 | uniq -c | tail -n 5
Rank Country Driver Races Wins
1 [United_Kingdom] Lewis_Hamilton 264 94
2 [Germany] Sebastian_Vettel 254 53
3 [Spain] Fernando_Alonso 311 32
4 [Finland] Kimi_Raikkonen 326 21
5 [Germany] Nico_Rosberg 206 23
6 [Germany] Michael_Schumacher 307 91
7 [Finland] Valtteri_Bottas 153 9
8 [United_Kingdom] Jenson_Button 306 15
9 [Brazil] Felipe_Massa 269 11
10 [Australia] Daniel_Ricciardo 185 7
11 [Netherlands] Max_Verstappen 116 9
12 [Australia] Mark_Webber 215 9
13 [France] Alain_Prost 199 51
14 [Mexico] Sergio_Perez 188 0
15 [Brazil] Rubens_Barrichello 322 11
16 [Brazil] Ayrton_Senna 161 41
17 [United_Kingdom] David_Coulthard 246 13
18 [Germany] Nico_Hulkenberg 180 0
19 [Brazil] Nelson_Piquet 204 23
20 [United_Kingdom] Nigel_Mansell 188 31
21 [Austria] Niki_Lauda 173 25
22 [Finland] Mika_Hakkinen 161 20
23 [Monaco] Charles_Leclerc 56 2
24 [France] Romain_Grosjean 178 0
25 [Austria] Gerhard_Berger 210 10
26 [United_Kingdom] Jackie_Stewart 100 27
27 [United_Kingdom] Damon_Hill 115 22
28 [Spain] Carlos_Sainz_Jr. 115 0
29 [Germany] Ralf_Schumacher 180 6
30 [Argentina] Carlos_Reutemann 146 12
31 [Colombia] Juan_Pablo_Montoya 94 7
32 [United_Kingdom] Graham_Hill 177 14
33 [Brazil] Emerson_Fittipaldi 145 14
34 [Italy] Riccardo_Patrese 256 6
35 [Argentina] Juan_Manuel_Fangio 58 24
36 [Italy] Giancarlo_Fisichella 230 3
37 [United_Kingdom] Jim_Clark 72 25
38 [Poland] Robert_Kubica 97 1
39 [Australia] Jack_Brabham 127 14
40 [Germany] Nick_Heidfeld 183 0
41 [South_Africa] Jody_Scheckter 112 10
42 [New_Zealand] Denny_Hulme 112 8
43 [Italy] Jarno_Trulli 252 1
44 [France] Jean_Alesi 201 1
45 [Canada] Jacques_Villeneuve 164 11
46 [France] Jacques_Laffite 176 6
47 [Switzerland] Clay_Regazzoni 131 5
48 [Australia] Alan_Jones 116 12
49 [Sweden] Ronnie_Peterson 123 10
50 [New_Zealand] Bruce_McLaren 102 4
51 [Russian_Federation] Daniil_Kvyat 107 0
52 [United_Kingdom] Eddie_Irvine 147 4
53 [France] Pierre_Gasly 61 1
54 [United_Kingdom] Stirling_Moss 72 16
55 [Italy] Michele_Alboreto 194 5
56 [Belgium] Jacky_Ickx 115 8
57 [France] Rene_Arnoux 149 7
58 [United_Kingdom] John_Surtees 111 6
59 [United_States] Mario_Andretti 128 12
60 [United_Kingdom] James_Hunt 92 10
61 [France] Esteban_Ocon 64 0
62 [Germany] Heinz-Harald_Frentzen 157 3
63 [United_Kingdom] John_Watson 152 5
64 [Thailand] Alexander_Albon 35 0
65 [Finland] Keke_Rosberg 114 5
66 [Denmark] Kevin_Magnussen 116 0
67 [France] Patrick_Depailler 95 2
68 [Italy] Alberto_Ascari 36 132
69 [United_States] Dan_Gurney 86 4
70 [Belgium] Thierry_Boutsen 163 3
71 [United_Kingdom] Mike_Hawthorn 48 3
72 [Italy] Nino_Farina 36 5
73 [Canada] Lance_Stroll 75 0
74 [Japan] Kamui_Kobayashi 75 0
75 [Germany] Adrian_Sutil 128 0
76 [United_Kingdom] Lando_Norris 35 0
77 [Italy] Elio_de_Angelis 109 2
78 [United_Kingdom] Paul_di_Resta 59 0
79 [Austria] Jochen_Rindt 60 6
80 [United_States] Richie_Ginther 52 1
81 [Canada] Gilles_Villeneuve 67 6
82 [Finland] Heikki_Kovalainen 111 1
83 [France] Patrick_Tambay 116 2
84 [France] Didier_Pironi 70 3
85 [United_States] Phil_Hill 51 3
86 [United_Kingdom] Martin_Brundle 158 0
87 [United_Kingdom] Johnny_Herbert 161 3
88 [France] Francois_Cevert 47 1
89 [Sweden] Stefan_Johansson 79 0
90 [New_Zealand] Chris_Amon 97 0
91 [Argentina] Jose_Froilan_Gonzalez 29 2
92 [France] Jean-Pierre_Beltoise 85 1
93 [France] Olivier_Panis 157 1
94 [United_Kingdom] Tony_Brooks 41 6
95 [Venezuela] Pastor_Maldonado 95 1
96 [France] Maurice_Trintignant 86 2
97 [Mexico] Pedro_Rodriguez 53 2
98 [Germany] Jochen_Mass 105 1
99 [United_Kingdom] Derek_Warwick 147 0
100 [United_States] Eddie_Cheever 132 0
101 [Switzerland] Jo_Siffert 97 2
102 [Italy] Alessandro_Nannini 77 1
103 [Russian_Federation] Vitaly_Petrov 57 0
104 [United_Kingdom] Peter_Revson 30 2
105 [Italy] Andrea_de_Cesaris 208 0
106 [Italy] Lorenzo_Bandini 44 1
107 [Brazil] Carlos_Pace 72 1
108 [Germany] Wolfgang_von_Trips 27 2
109 [France] Jean-Eric_Vergne 58 0
110 [France] Jean_Behra 57 0
111 [Germany] Timo_Glock 91 0
112 [Italy] Luigi_Villoresi 33 0
113 [United_Kingdom] Peter_Collins 36 3
114 [United_Kingdom] Innes_Ireland 52 1
115 [Austria] Alexander_Wurz 69 0
116 [Italy] Luigi_Musso 25 1
117 [Japan] Takuma_Sato 90 0
118 [Italy] Piero_Taruffi 18 1
119 [Sweden] Jo_Bonnier 106 1
120 [Spain] Pedro_de_la_Rosa 105 0
121 [Brazil] Bruno_Senna 46 0
122 [Finland] Mika_Salo 110 0
123 [Italy] Luigi_Fagioli 8 1
124 [United_Kingdom] Mark_Blundell 61 0
125 [United_States] Harry_Schell 63 0
126 [France] Jean-Pierre_Jarier 135 0
127 [Sweden] Gunnar_Nilsson 31 1
128 [Spain] Jaime_Alguersuari 46 0
129 [Italy] Ivan_Capelli 93 0
130 [United_States] Jim_Rathmann 12 1
131 [Brazil] Felipe_Nasr 39 0
132 [United_Kingdom] Mike_Hailwood 51 0
133 [Switzerland] Sebastien_Buemi 55 0
134 [Germany] Hans_Joachim_Stuck 74 0
135 [United_Kingdom] Mike_Spence 36 0
136 [South_Africa] Tony_Maggs 26 0
137 [Belgium] Stoffel_Vandoorne 41 0
138 [Italy] Vitantonio_Liuzzi 80 0
139 [Italy] Teo_Fabi 63 0
140 [United_States] Masten_Gregory 40 0
141 [France] Jean-Pierre_Jabouille 49 2
142 [United_States] Sam_Hanks 9 1
143 [United_Kingdom] Piers_Courage 27 0
144 [Italy] Eugenio_Castellotti 18 0
145 [United_States] Bill_Vukovich 5 2
146 [Brazil] Nelson_Piquet_Jr. 28 0
147 [United_Kingdom] Tom_Pryce 42 0
148 [United_Kingdom] Roy_Salvadori 48 0
149 [United_States] Jimmy_Bryan 9 1
150 [Belgium] Olivier_Gendebien 16 0
151 [Italy] Antonio_Giovinazzi 37 0
152 [France] Louis_Rosier 39 0
153 [Sweden] Marcus_Ericsson 97 0
154 [Italy] Pierluigi_Martini 119 0
155 [Italy] Felice_Bonetto 16 0
156 [Italy] Ludovico_Scarfiotti 11 1
157 [Germany] Karl_Kling 12 0
158 [Italy] Stefano_Modena 73 0
159 [Switzerland] Marc_Surer 82 0
160 [Netherlands] Jos_Verstappen 106 0
161 [United_Kingdom] Stuart_Lewis-Evans 14 0
162 [France] Robert_Manzon 28 0
163 [Japan] Satoru_Nakajima 74 0
164 [Italy] Vittorio_Brambilla 74 1
165 [Brazil] Roberto_Moreno 43 0
166 [Ireland] Derek_Daly 49 0
167 [United_Kingdom] Mike_Parkes 6 0
168 [United_States] Rodger_Ward 12 1
169 [Italy] Giancarlo_Baghetti 26 1
170 [Austria] Karl_Wendlinger 41 0
171 [Austria] Christian_Klien 49 0
172 [Germany] Rolf_Stommelen 55 0
173 [Italy] Bruno_Giacomelli 69 0
174 [United_Kingdom] Jonathan_Palmer 84 0
175 [Argentina] Roberto_Mieres 17 0
176 [Sweden] Reine_Wisell 23 0
177 [Brazil] Cristiano_da_Matta 28 0
178 [Mexico] Hector_Rebaque 41 0
179 [United_Kingdom] Jackie_Oliver 50 0
180 [United_States] Johnnie_Parsons 10 1
181 [United_Kingdom] Peter_Arundell 13 0
182 [Brazil] Christian_Fittipaldi 40 0
183 [France] Henri_Pescarolo 57 0
184 [Belgium] Paul_Frere 11 0
185 [United_States] Tony_Bettenhausen 13 0
186 [United_Kingdom] Cliff_Allison 16 0
187 [United_Kingdom] Richard_Attwood 17 0
188 [United_Kingdom] Peter_Gethin 30 1
189 [France] Philippe_Streiff 54 0
190 [Italy] Arturo_Merzario 57 0
191 [Switzerland] Rudi_Fischer 7 0
192 [United_States] Johnny_Thomson 9 0
193 [Germany] Hans_Herrmann 18 0
194 [New_Zealand] Howden_Ganley 36 0
195 [France] Eric_Bernard 45 0
196 [Finland] Jyrki_Jarvilehto 62 0
197 [Brazil] Mauricio_Gugelmin 74 0
198 [Brazil] Pedro_Diniz 99 0
199 [United_States] Troy_Ruttman 8 1
200 [United_States] Lee_Wallard 2 1
答案1
使用 awk,这将返回第 5 个字段(获胜)大于 50 或不是数字的所有条目。
awk '$5 > 50' f1.txt
你明白了
Rank Country Driver Races Wins
1 [United_Kingdom] Lewis_Hamilton 264 94
2 [Germany] Sebastian_Vettel 254 53
6 [Germany] Michael_Schumacher 307 91
13 [France] Alain_Prost 199 51
68 [Italy] Alberto_Ascari 36 132
因为在这种情况下,这是行中的最后一个字段,您也可以使用
awk '$NF > 50' f1.txt
如果你真的想使用 grep ,你可以使用类似这样的东西,来自 Greg Fenton 的答案,但你需要更改-E
为-P
使用 Perl 正则表达式,添加\b
以指示数字位于单词边界之后,因此没有数字或字母就在它之前,并将\d
三位数部分中的第一个更改为[1-9]
, 以不存在带前导 0 的三位数
grep -P '\b([5-9][1-9]|60|[1-9]\d\d)$' f1.txt
这是假设只有两位数或三位数的胜利。
\b表示单词边界,表示第一个数字之前不能有数字或字母
()意味着在单词边界之后你需要有括号内定义的内容。
[5-9][1-9]表示应该有两位数,其中第一个数字是 5-9,其他数字是 1-9,这意味着它将捕获数字 51-99,排除 60。
|意味着 or 运算符,并且由于 60 被排除在之前的正则表达式中,[5-9][1-9]
我们说 or 60。
|[1-9]\d\d表示或三位数字,其中第一位数字是 1-9 后跟任何两位数字,[1-9]\d\d
也可以是[1-9][0-9][0-9
]
$表示行尾,在最后一个数字之后应该有行尾,这样您就不会捕获不在最后一个字段中、后面没有行尾的数字。
所以基本上 grep 表示查找单词边界,然后是大于 50 的两位或三位数字,然后是行尾。
这部分[5-9][1-9]|60
查找两位数字 51-99,这部分[1-9]\d\d
查找三位数字,其中第一位数字是 1-9,如果您想使用多于三位数字,请添加另一个数字或使用|
like进行操作
grep -P '\b([5-9][1-9]|60|[1-9]\d\d|[1-9]\d\d\d)$' f1.txt
以上是四位数字。
如果您想使用任意数量的数字,请使用类似的内容
grep -P '\b([5-9][1-9]|60|[1-9]\d\d)\d*$' f1.txt
如果由于某种原因 Perl 正则表达式在你的 grep 中不起作用,你可以尝试扩展正则表达式
grep -E '\b([5-9][1-9]|60|[1-9][0-9][0-9])[0-9]*$' f1.txt
答案2
坚持使用的需要grep
,怎么样:
grep -E '([5-9]\d|\d\d\d)$' f1.txt
产生:
$ grep -E '([5-9]\d|\d\d\d)$' f1.txt
1 [United_Kingdom] Lewis_Hamilton 264 94
2 [Germany] Sebastian_Vettel 254 53
6 [Germany] Michael_Schumacher 307 91
13 [France] Alain_Prost 199 51
68 [Italy] Alberto_Ascari 36 132
答案3
对于grep
(如果你真的需要它awk
)那就是:
grep -E '[[:blank:]]0*(5[1-9]|[6-9][0-9]|[1-9][0-9][0-9])[0-9]*$' infile