Upcase a File – SotD

I just came up with something fun. Sotd – Script of the Day. Whenever I find/make a good script I will try to include it here, with the tag, and some fun content to work around it. Today’s script is the simple task of converting an entire file to uppercase. To standardize solutions I will say the current file is in.txt and we want the output file to go to out.txt. Here goes…

This simple task came from a question posted on Experts Exchange in the Scripting zone. Specifically Perl, however I putzed around with Perl and couldn’t quickly figure out what my print uc($_); was doing wrong, so I moved on to try my luck with Ruby. One minute without a need to look up any functions and 30 seconds of it not realizing I was still in the irb… and I had my Ruby solution:

> ruby -e "puts File.read('in.txt').upcase" > out.txt
> ruby -ne 'puts $_.upcase' < in.txt > out.txt

Which helped me produce the perl solution:

> perl -ne 'print uc' in.txt > out.txt

But I figured there had to be an easier way. I decided to manipulate tr///, transliterate, to convert lowercase characters to uppercase with the following one-liner piping the input file through tr and redirecting the output:

> cat in.txt | tr a-z A-Z > out.txt

I was pretty happy with it. How would you have done it?

Add a Comment

search