• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to convert text files to all upper or lower case

by admin

As usual, in Linux, there are more than 1 way to accomplish a task. To convert a file (input.txt) to all lower case (output.txt), choose any ONE of the following:

To convert a file (input.txt) to all lower case (output.txt)

1. dd: You may have used dd for many other purposes but it can be used for text conversions also.

$ dd if=input.txt of=output.txt conv=lcase

2. tr: You can translate all uppercase (A–Z) to lowercase characters (a-z) using the tr command and specifying a range of characters, as in:

There is also special syntax in tr for specifying this sort of range for upper and lower case conversions:

$ tr '[:upper:]' '[:lower:]' < input.txt > output.txt

3. awk: awk has a special function tolower for uppercase to lowercase conversion.

$ awk '{ print tolower($0) }' input.txt > output.txt

4. perl:

$ perl -pe '$_= lc($_)' input.txt > output.txt

5. sed:

$ sed -e 's/\(.*\)/\L\1/' input.txt > output.txt

We use the backreference \1 to refer to the entire line and the \L to convert to lower case.

To convert a file (input.txt) to all upper case (output.txt)

1. dd: Use below command to convert lowercase to uppercase.

$ dd if=input.txt of=output.txt conv=ucase

2. tr: You can translate all lowercase characters (a-z) to upercase (A–Z) using the tr command and specifying a range of characters, as in:

$ tr 'A-Z' 'a-z' < input.txt > output.txt

There is also special syntax in tr for specifying this sort of range for upper-and lower-case conversions:

$ tr '[:lower:]' '[:upper:]' < input.txt > output.txt

3. awk: awk has special function toupper for lowercase to uppercase conversion.

$ awk '{ print toupper($0) }' input.txt > output.txt

4. perl:

$ perl -pe '$_= uc($_)' input.txt > output.txt

5. sed:

$ sed -e 's/\(.*\)/\U\1/' input.txt > output.txt

Filed Under: DevOps, Linux, Shell Scripting

Some more articles you might also be interested in …

  1. logger Command Examples in Linux
  2. How to install virtual machines optimized and configured for the Red Hat Virtualization environment
  3. CentOS / RHEL 6 : How to force a NTP sync with the NTP server(s)
  4. Allow cronjobs to run by pam even if user password is expired
  5. CentOS / RHEL : How to add swap file
  6. groupmems Command Examples in Linux
  7. How To Calculate The Memory Reserved By HugePages in CentOS/RHEL
  8. Understanding the /etc/exports File
  9. “530 Non-anonymous sessions must use encryption” – while using curl
  10. grub-mkconfig Command Options

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright