52

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?

1

6 Answers 6

98

Try this:

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

Using a file:

awk '{ print toupper($0) }' yourfile.txt
5
  • 4
    take note, special chars will fail : ```awk '{ print toupper($0) }' <<< stéphane
    – SvennD
    Dec 4, 2015 at 9: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. Sep 6, 2016 at 17:11
  • Generally gawk should handle this fine. Other awks never really made efforts to support i18n features.
    – jena
    Apr 13, 2022 at 11:43
  • @jena do you mean that gawk will not have the issue with special characters? Feb 19 at 8: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 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
6
  • 2
    Note that (as of coreutils 8.23) it will fail to convert accentuated characters. Sep 6, 2016 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, 2022 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! Apr 13, 2022 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, 2022 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. Apr 13, 2022 at 21:13
4

Try this:

$ echo mix23xsS | awk '{ print toupper($0) }'
MIX23XSS
0
3

Something like

< yourMIXEDCASEfile.txt awk '{print toupper($0)}' > yourUPPERCASEfile.txt
2
  • 3
    Ok... how about awk '{print toupper($0)}' yourMIXEDCASEfile.txt > yourUPPERCASEfile.txt
    – Silviu
    Dec 24, 2012 at 13:18
  • 1
    it was not i to downvote your post, but the second solution, without cat, is way better
    – Rubens
    Dec 24, 2012 at 13:19
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)

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.