如何使用awk将文件的某个字段更改为大写? [英] How can I change a certain field of a file into upper-case using awk?

查看:217
本文介绍了如何使用awk将文件的某个字段更改为大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的文本文件:

I have a text file like this:

1 http http 3 4 5 
2 dns dns 4 
3 ftp ftp 4 5 6 8 

我希望输出像这样:

1 HTTP http 3 4 5 
2 DNS dns 4 
3 FTP ftp 4 5 6 8 

我想将第二个字段从小写更改为大写,仅将第二个字段更改.

I want to change the second field from lower-case into upper-case and only the second field.

请注意,特定行中的字段数不是固定的.

Note that the number of fields in a certain line is not fixed.

我可以使用awk完成这个目标吗?

Can I accomplish this goal using awk?

推荐答案

您肯定可以.方法如下:

You sure can. Here's how:

awk '$2 = toupper($2)' file

结果:

1 HTTP http 3 4 5
2 DNS dns 4
3 FTP ftp 4 5 6 8

从手册中:

tolower(str)

tolower(str)

返回字符串str的副本,所有大写形式 str中的字符转换为它们对应的小写字母 同行.非字母字符保持不变.

Returns a copy of the string str, with all the upper-case characters in str translated to their corresponding lower-case counterparts. Non-alphabetic characters are left unchanged.

toupper(str)

toupper(str)

返回字符串str的副本,所有小写形式 str中的字符转换为它们对应的大写字母 同行.非字母字符保持不变.

Returns a copy of the string str, with all the lower-case characters in str translated to their corresponding upper-case counterparts. Non-alphabetic characters are left unchanged.


我假设给定您的样本数据,它将至少有三列,并且您讨论的变量组件适用于包含单词的列之后的列.如果我错了,您可以简单地添加条件:


I made the assumption that given your sample data, it would have at least three columns and that the variable component you discuss applies to the columns after those containing words. If I was wrong, you can simply add the conditional:

awk 'NF>1 { $2 = toupper($2) }1' file

这篇关于如何使用awk将文件的某个字段更改为大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆