Linux
SED - 如何排除與文本在同一行中的特殊字元?
名稱以 L 或 S 開頭的司機,附加“WINNER”。
例如:
從:
76
$$ United Kingdom $$蘭多·諾里斯 35 0
至
76
$$ United Kingdom $$獲勝者蘭多·諾里斯 35 0
我正在使用的命令
cat f1.txt | sed -r 's/] [L|S][A-Za-z][ A-Za-z-]*\b/WINNER&/'
輸出錯誤:
76 [United KingdomWINNER] LANDO NORRIS 35 0
完整列表:
Rank Country Driver Races Wins 1 [United Kingdom] LEWIS HAMILTON 264 94 3 [Spain] Fernando Alonso 311 32 8 [United Kingdom] JENSON BUTTON 306 15 11 [Netherlands] Max Verstappen 116 9 17 [United Kingdom] DAVID COULTHARD 246 13 20 [United Kingdom] NIGEL MANSELL 188 31 26 [United Kingdom] JACKIE STEWART 100 27 27 [United Kingdom] DAMON HILL 115 22 28 [Spain] Carlos Sainz Jr. 115 0 32 [United Kingdom] GRAHAM HILL 177 14 37 [United Kingdom] JIM CLARK 72 25 38 [Poland] Robert Kubica 97 1 41 [South Africa] Jody Scheckter 112 10 42 [New Zealand] Denny Hulme 112 8 47 [Switzerland] Clay Regazzoni 131 5 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 54 [United Kingdom] STIRLING MOSS 72 16 58 [United Kingdom] JOHN SURTEES 111 6 59 [United States] MARIO ANDRETTI 128 12 60 [United Kingdom] JAMES HUNT 92 10 63 [United Kingdom] JOHN WATSON 152 5 64 [Thailand] Alexander Albon 35 0 69 [United States] DAN GURNEY 86 4 71 [United Kingdom] MIKE HAWTHORN 48 3 76 [United Kingdom] LANDO NORRIS 35 0 78 [United Kingdom] PAUL DI RESTA 59 0 80 [United States] RICHIE GINTHER 52 1 85 [United States] PHIL HILL 51 3 86 [United Kingdom] MARTIN BRUNDLE 158 0 87 [United Kingdom] JOHNNY HERBERT 161 3 89 [Sweden] Stefan Johansson 79 0 90 [New Zealand] Chris Amon 97 0 94 [United Kingdom] TONY BROOKS 41 6 95 [Venezuela] Pastor Maldonado 95 1 99 [United Kingdom] DEREK WARWICK 147 0 100 [United States] EDDIE CHEEVER 132 0 101 [Switzerland] Jo Siffert 97 2 103 [Russian Federation] Vitaly Petrov 57 0 104 [United Kingdom] PETER REVSON 30 2 113 [United Kingdom] PETER COLLINS 36 3 114 [United Kingdom] INNES IRELAND 52 1 119 [Sweden] Jo Bonnier 106 1 120 [Spain] Pedro de la Rosa 105 0 124 [United Kingdom] MARK BLUNDELL 61 0 125 [United States] HARRY SCHELL 63 0 127 [Sweden] Gunnar Nilsson 31 1 128 [Spain] Jaime Alguersuari 46 0 130 [United States] JIM RATHMANN 12 1 132 [United Kingdom] MIKE HAILWOOD 51 0 133 [Switzerland] Sebastien Buemi 55 0 135 [United Kingdom] MIKE SPENCE 36 0 136 [South Africa] Tony Maggs 26 0 140 [United States] MASTEN GREGORY 40 0 142 [United States] SAM HANKS 9 1 143 [United Kingdom] PIERS COURAGE 27 0 145 [United States] BILL VUKOVICH 5 2 147 [United Kingdom] TOM PRYCE 42 0 148 [United Kingdom] ROY SALVADORI 48 0 149 [United States] JIMMY BRYAN 9 1 153 [Sweden] Marcus Ericsson 97 0 159 [Switzerland] Marc Surer 82 0 160 [Netherlands] Jos Verstappen 106 0 161 [United Kingdom] STUART LEWIS-EVANS 14 0 167 [United Kingdom] MIKE PARKES 6 0 168 [United States] RODGER WARD 12 1 174 [United Kingdom] JONATHAN PALMER 84 0 176 [Sweden] Reine Wisell 23 0 179 [United Kingdom] JACKIE OLIVER 50 0 180 [United States] JOHNNIE PARSONS 10 1 181 [United Kingdom] PETER ARUNDELL 13 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 191 [Switzerland] Rudi Fischer 7 0 192 [United States] JOHNNY THOMSON 9 0 194 [New Zealand] Howden Ganley 36 0 199 [United States] TROY RUTTMAN 8 1 200 [United States] LEE WALLARD 2 1
在這種情況下,您不需要打開擴展的正則表達式。此外,方括號強加了元素的隱含 ORing,因此管道充當另一個元素。
$ sed -e ' s/] [LS]/\ &/ s/\n]/] WINNER/ ' f1.txt
$ sed 's/]\( [LS]\)/] WINNER\1/' file Rank Country Driver Races Wins 1 [United Kingdom] WINNER LEWIS HAMILTON 264 94 3 [Spain] Fernando Alonso 311 32 8 [United Kingdom] JENSON BUTTON 306 15 11 [Netherlands] Max Verstappen 116 9 17 [United Kingdom] DAVID COULTHARD 246 13 20 [United Kingdom] NIGEL MANSELL 188 31 26 [United Kingdom] JACKIE STEWART 100 27 27 [United Kingdom] DAMON HILL 115 22 28 [Spain] Carlos Sainz Jr. 115 0 32 [United Kingdom] GRAHAM HILL 177 14 37 [United Kingdom] JIM CLARK 72 25 38 [Poland] Robert Kubica 97 1 41 [South Africa] Jody Scheckter 112 10 42 [New Zealand] Denny Hulme 112 8 47 [Switzerland] Clay Regazzoni 131 5 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 54 [United Kingdom] WINNER STIRLING MOSS 72 16 58 [United Kingdom] JOHN SURTEES 111 6 59 [United States] MARIO ANDRETTI 128 12 60 [United Kingdom] JAMES HUNT 92 10 63 [United Kingdom] JOHN WATSON 152 5 64 [Thailand] Alexander Albon 35 0 69 [United States] DAN GURNEY 86 4 71 [United Kingdom] MIKE HAWTHORN 48 3 76 [United Kingdom] WINNER LANDO NORRIS 35 0 78 [United Kingdom] PAUL DI RESTA 59 0 80 [United States] RICHIE GINTHER 52 1 85 [United States] PHIL HILL 51 3 86 [United Kingdom] MARTIN BRUNDLE 158 0 87 [United Kingdom] JOHNNY HERBERT 161 3 89 [Sweden] WINNER Stefan Johansson 79 0 90 [New Zealand] Chris Amon 97 0 94 [United Kingdom] TONY BROOKS 41 6 95 [Venezuela] Pastor Maldonado 95 1 99 [United Kingdom] DEREK WARWICK 147 0 100 [United States] EDDIE CHEEVER 132 0 101 [Switzerland] Jo Siffert 97 2 103 [Russian Federation] Vitaly Petrov 57 0 104 [United Kingdom] PETER REVSON 30 2 113 [United Kingdom] PETER COLLINS 36 3 114 [United Kingdom] INNES IRELAND 52 1 119 [Sweden] Jo Bonnier 106 1 120 [Spain] Pedro de la Rosa 105 0 124 [United Kingdom] MARK BLUNDELL 61 0 125 [United States] HARRY SCHELL 63 0 127 [Sweden] Gunnar Nilsson 31 1 128 [Spain] Jaime Alguersuari 46 0 130 [United States] JIM RATHMANN 12 1 132 [United Kingdom] MIKE HAILWOOD 51 0 133 [Switzerland] WINNER Sebastien Buemi 55 0 135 [United Kingdom] MIKE SPENCE 36 0 136 [South Africa] Tony Maggs 26 0 140 [United States] MASTEN GREGORY 40 0 142 [United States] WINNER SAM HANKS 9 1 143 [United Kingdom] PIERS COURAGE 27 0 145 [United States] BILL VUKOVICH 5 2 147 [United Kingdom] TOM PRYCE 42 0 148 [United Kingdom] ROY SALVADORI 48 0 149 [United States] JIMMY BRYAN 9 1 153 [Sweden] Marcus Ericsson 97 0 159 [Switzerland] Marc Surer 82 0 160 [Netherlands] Jos Verstappen 106 0 161 [United Kingdom] WINNER STUART LEWIS-EVANS 14 0 167 [United Kingdom] MIKE PARKES 6 0 168 [United States] RODGER WARD 12 1 174 [United Kingdom] JONATHAN PALMER 84 0 176 [Sweden] Reine Wisell 23 0 179 [United Kingdom] JACKIE OLIVER 50 0 180 [United States] JOHNNIE PARSONS 10 1 181 [United Kingdom] PETER ARUNDELL 13 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 191 [Switzerland] Rudi Fischer 7 0 192 [United States] JOHNNY THOMSON 9 0 194 [New Zealand] Howden Ganley 36 0 199 [United States] TROY RUTTMAN 8 1 200 [United States] WINNER LEE WALLARD 2 1