Advertisement
chotoipho

BASH: multiple ways to convert to/from lower/uppercase

Jun 4th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.13 KB | None | 0 0
  1.  How to convert text files to all upper or lower case
  2.  
  3. How can you convert a text file to all lower case or all upper case?
  4.  
  5. As usual, in Linux, there are more than 1 way to accomplish a task.
  6.  
  7. To convert a file (input.txt) to all lower case (output.txt), choose any ONE of the following:
  8.  
  9.  
  10.     dd
  11.  
  12.     $ dd if=input.txt of=output.txt conv=lcase
  13.  
  14.  
  15.     tr
  16.  
  17.     $ tr '[:upper:]' '[:lower:]' < input.txt > output.txt
  18.  
  19.  
  20.     awk
  21.  
  22.     $ awk '{ print tolower($0) }' input.txt > output.txt
  23.  
  24.  
  25.     perl
  26.  
  27.     $ perl -pe '$_= lc($_)' input.txt > output.txt
  28.  
  29.  
  30.     sed
  31.  
  32.     $ sed -e 's/\(.*\)/\L\1/' input.txt > output.txt
  33.  
  34.  
  35.     We use the backreference \1 to refer to the entire line and the \L to convert to lower case.
  36.  
  37.  
  38.  
  39. To convert a file (input.txt) to all upper case (output.txt):
  40.  
  41.  
  42.     dd
  43.  
  44.     $ dd if=input.txt of=output.txt conv=ucase
  45.  
  46.  
  47.     tr
  48.  
  49.     $ tr '[:lower:]' '[:upper:]' < input.txt > output.txt
  50.  
  51.  
  52.     awk
  53.  
  54.     $ awk '{ print toupper($0) }' input.txt > output.txt
  55.  
  56.  
  57.     perl
  58.  
  59.     $ perl -pe '$_= uc($_)' input.txt > output.txt
  60.  
  61.  
  62.     sed
  63.  
  64.     $ sed -e 's/\(.*\)/\U\1/' input.txt > output.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement