54

I have a file mixed with lower-case letters and upper-case letters, can I use awk to convert all the letters in that file into upper-case?

Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
  • duplicated/related question: https://stackoverflow.com/questions/1538676/uppercasing-first-letter-of-words-using-sed – jian Jan 13 '22 at 11:22

6 Answers6

101

Try this:

awk '{ print toupper($0) }' <<< "your string"

Using a file:

awk '{ print toupper($0) }' yourfile.txt
Rubens
  • 14,478
  • 11
  • 63
  • 92
  • 4
    take note, special chars will fail : ```awk '{ print toupper($0) }' <<< stéphane – SvennD Dec 04 '15 at 09:13
  • 4
    @SvennD Depends on the version — doesn't work with mawk 1.3.3, but seems to work fine with GNU Awk 4.0.1 and 4.1.1. – Skippy le Grand Gourou Sep 06 '16 at 17:11
  • Generally `gawk` should handle this fine. Other awks never really made efforts to support i18n features. – jena Apr 13 '22 at 11:43
  • @jena do you mean that gawk will not have the issue with special characters? – experiment unit 1998X Feb 19 '23 at 08:41
  • 1
    @experimentunit1998X Yeah, I just tested it with the row of special chars on my Czech keyboard and they were all converted properly to uppercase by `gawk`, while `mawk` (1.3.4) didn't touch any of them and printed them in original lowercase form. – jena Feb 23 '23 at 13:25
21

You can use awk, but tr is the better tool:

tr a-z A-Z < input

or

tr [:lower:] [:upper:] < input
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 2
    Note that (as of coreutils 8.23) it will fail to convert accentuated characters. – Skippy le Grand Gourou Sep 06 '16 at 17:17
  • Well, `tr` is not better if I need to do it e.g. on one column of a table (while keeping the rest of the table unmodified). There are reasons for `awk` specific questions ;) – jena Apr 13 '22 at 11:46
  • 1
    @jena Quite often the reason people ask tool specific questions is that they are unaware of the existence of other tools! – William Pursell Apr 13 '22 at 13:28
  • I think I build my intuition about which tool to use in a similar way. And by searching for `awk` solution for my larger `awk` script, I arrived here :) worked great :) But I also learned about a new use for `tr`, so thanks! It's just this time I was here for `awk`, and so I got triggered by "`tr` is better" :D – jena Apr 13 '22 at 21:02
  • 1
    @jena Ah, yes, perhaps I should explicitly state that `tr` is the better tool *for this particular problem*. `awk` is certainly more flexible! I did not in any way intend to denigrate `awk`. Indeed, `tr` is quite limited. – William Pursell Apr 13 '22 at 21:13
  • No worries, you did answer the OP, sorry about jumping up like that :D – jena Apr 13 '22 at 22:23
4

Try this:

$ echo mix23xsS | awk '{ print toupper($0) }'
MIX23XSS
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
basos
  • 578
  • 4
  • 11
3

Something like

< yourMIXEDCASEfile.txt awk '{print toupper($0)}' > yourUPPERCASEfile.txt
William Pursell
  • 204,365
  • 48
  • 270
  • 300
Silviu
  • 835
  • 14
  • 22
2

You mean like this thread explains: http://www.unix.com/shell-programming-scripting/24320-converting-file-names-upper-case.html (Ok, it's about filenames, but the same principle applies to files)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
-1

If Perl is an option:

perl -ne 'print uc()' file
  • -n loop around input file, do not automatically print line
  • -e execute the perl code in quotes
  • uc() = uppercase

To print all lowercase:

perl -ne 'print lc()' file
Chris Koknat
  • 3,305
  • 2
  • 29
  • 30