[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4496
  • Last Modified:

Convert contents of txt file to upper case using Perl

I need to read the contents of a text or xml file and convert all lower case letters in the contents of the file to upper case. Is there a simple way using Perl that I could accomplish this.
  • 2
  • 2
1 Solution
perl -ne "print uc;" originalfile.txt > uppercasefile.txt
FYI:  the single line did not work for me.. gives

Not enough arguments for upper case at -e line 1, near "uc;"
Execution of -e aborted due to compilation errors.

Anyhow.. you could do it in one of following ways:
Create a small perl scripts and have
  print uc($_);

or use awk

awk '{print tolower($0)}' originalfile.txt
Or you can use ruby: (you may only supply 1 argument for in.txt)
> ruby -e "puts File.read(ARGV.to_s).upcase" in.txt > out.txt

Or specify the file right in the script:
> ruby -e "puts File.read('in.txt').upcase" > out.txt

But as for Perl, I was surprised it gave so much trouble, so I went with some more fun ways to do it, and I found this to be a pretty cool solution:
> cat in.txt | tr a-z A-Z > out.txt
khota001 -- It sounds like you're running an extremely old and broken version of perl.  You might want to upgrade.  Googling around it looks like that was fixed way back in February of '97 in perl v.5.003_28.  See the thread titled "uc without explicit argument..." in the following:


Under a modern perl version, the uc function defaults to $_ if no argument is given.

$ perldoc -f uc

       uc EXPR
       uc      Returns an uppercased version of EXPR.  This is the internal
               function implementing the "\U" escape in double-quoted strings.
               Respects current LC_CTYPE locale if "use locale" in force.  See
               perllocale and perlunicode for more details about locale and
               Unicode support.  It does not attempt to do titlecase mapping
               on initial letters.  See "ucfirst" for that.

               If EXPR is omitted, uses $_.

And the -n switch:

  -n              assume "while (<>) { ... }" loop around program

So, the perl code you posted while (<>) { print uc($_); } should be the same as my earlier post (perl -ne "print uc;") on an unbroken perl build (which should be any perl released within the last 10 years or so).  
I agree.. the PERL version at work is really old... after i tried your solution I read docs.. and found out that it should work on a newer version..

but just in case dlnewman70 was using an older version I provided my 2 cents...

Unfortunately upgrading the PERL version at work in not in my hands :(

Thanks for the tip though....

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now