Convert contents of txt file to upper case using Perl

Posted on 2007-07-31
Last Modified: 2010-08-05
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.
Question by:dlnewman70
    LVL 25

    Accepted Solution

    perl -ne "print uc;" originalfile.txt > uppercasefile.txt
    LVL 1

    Expert Comment

    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
    LVL 17

    Expert Comment

    Or you can use ruby: (you may only supply 1 argument for in.txt)
    > ruby -e "puts" in.txt > out.txt

    Or specify the file right in the script:
    > ruby -e "puts'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
    LVL 25

    Expert Comment

    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).  
    LVL 1

    Expert Comment

    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....

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now