SED - 텍스트와 같은 줄에서 특수 문자를 제외하는 방법은 무엇입니까?

SED - 텍스트와 같은 줄에서 특수 문자를 제외하는 방법은 무엇입니까?

이름이 L 또는 S로 시작하는 드라이버는 "WINNER"를 추가하세요.

예를 들어:

에서:

76 [영국] 랜도 노리스 35 0

도착하다

76 [영국] 우승자 랜도 노리스 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

답변1

$ 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

답변2

이 경우 확장 정규식을 활성화할 필요가 없습니다. 또한 대괄호는 요소의 암시적 ORing을 적용하므로 파이프가 다른 요소처럼 작동합니다.

$ sed -e '
    s/] [LS]/\
&/
    s/\n]/] WINNER/
' f1.txt

관련 정보