如何将列表中的特定文本转换为大写?

如何将列表中的特定文本转换为大写?

我想将来自以“ United”开头的国家/地区的所有驱动程序名称都大写。

例如:

20  [United Kingdom] Nigel Mansell                 188 31

20  [United Kingdom] NIGEL MANSELL                 188 31

我正在使用的命令:

cat f1.txt | sed -r 's/[^ ]"United"\s+[A-Z]+[a-z]*]\s+[A-Z]+[a-z]*\s+[A-Z]+[a-z]*-?[A-Z]?+[a-z]?*/\U&/g'

完整列表:

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

你做得太过分了,利用你的文件结构来保持简单!

如果我们在一行中找到该字符串[United,则将从右大括号到行尾的所有内容都大写即可给出​​您想要的结果。将其翻译成 Sed 语言,

sed '/\[United/s/].*/\U&/' file

请注意,以上内容特定于 GNU Sed。如果不可用但在 POSIX 系统中,您可以使用前任(或参见αГsнιη 的 Awk 版本) 具有类似的语法:

printf '%s\n' 'g/\[United/s/].*/\U&/' '%p' | ex file

要将更改保存到文件而不是打印结果,请更改%px.

答案2

使用awk

awk -F"[][]" '$2 ~/^United/ { $2="["$2"]"; $3=toupper($3); }1' OFS='' infile

[...]我们将]和定义[为字段分隔符之后,检查第二个字段是否以United文本开头,如果是,则在第二个字段周围添加回来[](我假设没有其他列倾斜这些字符,否则这些字段将错过这一点);然后我们将第三列转换为其大写值;习惯用法1是触发awk默认打印(一种始终为真的条件)。

答案3

当该记录的第二个字段中有 United 时,我们awk可以通过首先确定当前记录中右方括号的位置来做到这一点。

$ awk '
    $2 == "[United" {
      p = match($0, /]/)
      $0 = substr($0, 1, p-1) \
        toupper(substr($0, p))
    }1
' f1.txt

Posixly sed 可以按照与 awk 解决方案相同的思路来完成此操作。我们需要获取]位置并标记它,以便将模式空间分为两半。右半部分有名称,我们通过 y/// 命令将其大写。

$ sed -e "
    /\[United[[:blank:]]/!b
   y/]/\n/
    h;s/.*\n//
    y/$(printf %s {a..z})/$(printf %s {A..Z})/
    x;s/\n.*//
    G
    y/\n/]/
" f1.txt
$ perl -pe 's/\[United\s+.*?]\K(.*)/\U$1/' f1.txt

相关内容