Wednesday, August 22, 2007

string manipulation using tr linux command

tr a linux command that translate, squeeze, and/or delete characters from standard input, writing to standard output.

tr is one linux string manipulation tool that is installed by default installation with fedora boxes. tr currently focuses on deleting, squeezing, or translating string on a character per character basis or single byte characters.


tr set1 set2

set1 is any single byte character to be translated, squeezed, or deleted. set1 can be a set of CLASS blob characters.
set2 is the resulting translated single byte character

blob characters are:

[CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0
[:alnum:] all letters and digits
[:alpha:] all letters
[:blank:] all horizontal whitespace
[:cntrl:] all control characters
[:digit:] all digits
[:graph:] all printable characters, not including space
[:lower:] all lower case letters
[:print:] all printable characters, including space
[:punct:] all punctuation characters
[:space:] all horizontal or vertical whitespace
[:upper:] all upper case letters
[:xdigit:] all hexadecimal digits
[=CHAR=] all characters which are equivalent to CHAR
\\ backslash
\a audible BEL
\b backspace
\f form feed
\n new line
\r return
\t horizontal tab
\v vertical tab

Character translation occurs if -d is not passed as a parameter. -d parameter tells tr to delete the search blob character.


How to convert all lowercase to uppercase letters of text file?

Assuming we have testfile.txt that contains the following text data

# cat testfile.txt
i am vertito
this IS a text file

To convert small letters to upper letters, this can be done as follows

# cat testfile.txt | tr "a-z" "A-Z"
# cat testfile.txt | tr [:lower:] [:upper:]

which gives a resulting output of

and of course, converting upper letters to small letters would be like so

# cat testfile.txt | tr [:upper:] [:lower:]

How to replace a single character with a different character?

# cat testfile.txt | tr a @
i @m vertito
this IS @ text file

How to delete specified character from text files?

# cat testfile.txt | tr -d e
i am vrtito
this IS a txt fil

How to squeeze and delete repeated single byte character from text file?

Assuming we have a test.txt with the below data contents:

Sample test.txt file

# cat test.txt
I amm testing this mmessage
I ammm testing this mmmessage

Squeezing and deleting repeated letters would be done with the next command.

For the above sample, we need to squeeze repeated character 'm'

# cat test.txt | tr -s m
I am testing this message
I am testing this message



Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy