1

I want to know the command that will convert upper-case characters to lower-case characters and lower-case characters to upper-case characters.

It has to work in the shell of Unix machines.

Gandaro
  • 3,427
  • 1
  • 17
  • 19
Rock
  • 157
  • 1
  • 3
  • 13

4 Answers4

4
tr A-Za-z a-zA-Z

(unfortunately, ascii only)

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • Please can you elaborate more... i want to know the details syntax – Rock Jan 04 '13 at 21:53
  • 2
    Try to say man tr in your terminal. You will learn something new. – ssegvic Jan 04 '13 at 21:55
  • There is a command `tr` which maps character ranges to other character ranges (it reads standard input and writes to standard output). Here, we specified "from A to Z + from a to z" as a source range, "from a to z then from A to Z" as a destination range, so english letters are mapped to their case-inverted equivalents. – Anton Kovalenko Jan 04 '13 at 21:55
  • my file contents are HELLO sir...so i want output as hello SIR – Rock Jan 04 '13 at 21:59
  • That's exactly what you get for `tr A-Za-z a-zA-z < yourfile.txt` – Anton Kovalenko Jan 04 '13 at 22:00
3
cat file1 | tr 'a-zA-Z' 'A-Za-z'
David
  • 6,462
  • 2
  • 25
  • 22
  • not working.. my file contents are HELLO sir...so i want output as hello SIR – Rock Jan 04 '13 at 21:58
  • @Amar Hmmmm... works fine for me. – David Jan 04 '13 at 22:00
  • @Amar I think I did :-). `cat file1` gets the contents of file1, which are then piped to `tr`, which swaps upper and lower case, and then the output goes to STDOUT. Was there something else that you wanted to do? – David Jan 04 '13 at 22:09
  • I got the answer.. I am still beginner.. so trying hard to learn.. expecting your kind cooperation – Rock Jan 04 '13 at 22:11
  • +1 except you don't need `cat`: `tr 'a-zA-Z' 'A-Za-z' < file1` – glenn jackman Jan 04 '13 at 22:52
  • @glennjackman True. I think that's what Anton said in the comments below his answer. – David Jan 04 '13 at 23:02
0

You could pipe your file into perl with the answer provided here:

Perl, using tr function to convert uppercase to lowercase and vice-versa at the same time?

Community
  • 1
  • 1
grep
  • 726
  • 7
  • 13
  • if ($fred =~ /\A[A-Z_]\w*\z/i) { # Do nothing } else { print "The value of \$fred doesn't look like a Perl identifier name.\n"; } How the expression in if condition. – Rock Jan 08 '13 at 06:26
0

To get lowercase: sed -i 's/\(.*\)/\L\1/' somefilename

To get uppercase: sed -i 's/\(.*\)/\U\1/' somefilename

Brian Minton
  • 3,377
  • 3
  • 35
  • 41
  • I want to know 's/\(.*\)/\U\1/' how this syntax works? – Rock Jan 04 '13 at 22:07
  • `s/b/b/` replaces all occurrences of string a with string b. `\(` means the start of a chunk that will be used later. `\)` means the end of a chunk. `.*` is a regexp (regular expression) that matches 0 or more (i.e. any) characters. After the second `/`, the `\U` means uppercase, and the `\1` means the first chunk (called a backreference) from earlier in the expression. – Brian Minton Jan 04 '13 at 22:09
  • can you simplify more about \1 – Rock Jan 04 '13 at 22:10
  • sed processes its input one line at a time, so `.*` is a regexp that matches all characters in the line. For more info on regular expressions, check out http://www.regular-expressions.info/tutorial.html – Brian Minton Jan 04 '13 at 22:14
  • I just realized that this doesn't answer the original question, simultaneously converting uppercase to lowercase and lowercase to uppercase. See David's answer below for that. – Brian Minton Jan 04 '13 at 22:19
  • yes.. david's answer works fine – Rock Jan 04 '13 at 22:20
  • Hi david, brian can you tell me how this expression will work in perl if ($fred =~ /\A[A-Z_]\w*\z/i) { # Do nothing } else { print "The value of \$fred doesn't look like a Perl identifier name.\n"; } – Rock Jan 08 '13 at 06:23