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?
6 Answers
Try this:
awk '{ print toupper($0) }' <<< "your string"
Using a file:
awk '{ print toupper($0) }' yourfile.txt
-
4take note, special chars will fail : ```awk '{ print toupper($0) }' <<< stéphane– SvennDDec 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.– jenaApr 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
, whilemawk
(1.3.4) didn't touch any of them and printed them in original lowercase form.– jenaFeb 23 at 13:25
You can use awk
, but tr
is the better tool:
tr a-z A-Z < input
or
tr [:lower:] [:upper:] < input
-
2Note 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 forawk
specific questions ;)– jenaApr 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 largerawk
script, I arrived here :) worked great :) But I also learned about a new use fortr
, so thanks! It's just this time I was here forawk
, and so I got triggered by "tr
is better" :D– jenaApr 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 denigrateawk
. Indeed,tr
is quite limited. Apr 13, 2022 at 21:13
Something like
< yourMIXEDCASEfile.txt awk '{print toupper($0)}' > yourUPPERCASEfile.txt
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)
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 quotesuc()
= uppercase
To print all lowercase:
perl -ne 'print lc()' file