tr

translate or delete characters

Syntax

tr [OPTION]... SET1 [SET2]

Description

Translate, squeeze, and/or delete characters from standard input, writing to standard output.

Options

-c, -C, --complement
       use the complement of SET1

-d, --delete
       delete characters in SET1, do not translate

-s, --squeeze-repeats
       replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character

-t, --truncate-set1
       first truncate SET1 to length of SET2

Sets

\NNN   character with octal value NNN (1 to 3 octal digits)

\\     backslash

\a     audible BEL

\b     backspace

\f     form feed

\n     new line

\r     return

\t     horizontal tab

\v     vertical tab

CHAR1-CHAR2
       all characters from CHAR1 to CHAR2 in ascending order

[CHAR*]
       in SET2, copies of CHAR until length of SET1

[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

Example 1

Translate all the characters of a file from lower case to upper case using tr command.

[eleiss@eleiss ~]$ cat legends 
Mahatma Gandhiji
Lal Bahadur Shashtri
Sardar Vallabhbhai Patel
Gopalkrishna Gokhle
Bhagat Sinh
Lokmanya Tilak
[eleiss@eleiss ~]$ cat legends | tr [a-z] [A-Z]
MAHATMA GANDHIJI
LAL BAHADUR SHASHTRI
SARDAR VALLABHBHAI PATEL
GOPALKRISHNA GOKHLE
BHAGAT SINH
LOKMANYA TILAK
[eleiss@eleiss ~]$ cat legends | tr [:lower:] [:upper:]
MAHATMA GANDHIJI
LAL BAHADUR SHASHTRI
SARDAR VALLABHBHAI PATEL
GOPALKRISHNA GOKHLE
BHAGAT SINH
LOKMANYA TILAK
[eleiss@eleiss ~]$ 

The file "legends" containing names of some of the legends of India is displayed on the standard output using the cat command.

As we can see some alphabetical characters are in lower case while the others are in upper case. There are two illustrations given here that translate lower alphabetical characters to the upper ones using tr command. The first tr command specifies the range of characters while the second one specifies the sets.

Example 2

Translate a single character to another character in a file using tr command.

[eleiss@eleiss ~]$ cat birth_info 
Jul,1969 J'lo
Aug,1984 Alizee
Aug,1958 Madonna
Feb,1977 Shakira
Feb,1983 Rihana
[eleiss@eleiss ~]$ cat birth_info | tr " " "-"
Jul,1969-J'lo
Aug,1984-Alizee
Aug,1958-Madonna
Feb,1977-Shakira
Feb,1983-Rihana
[eleiss@eleiss ~]$

The content of the file "birth_info" is displayed using cat command. It is a two fields file. The fields are separated by a space character.

Later the tr command translates the space character into hyphen "-" character.

Example 3

Display name and permissions of all the files in current directory.

[eleiss@eleiss ~]$ ls -l
total 24104
-rw-------. 1 eleiss eleiss    14653 Feb  3  2011 autosave.xmi
drwxr-xr-x. 2 eleiss eleiss     4096 Sep 29  2010 bin
-rw-rw-r--. 1 eleiss eleiss       80 Sep 15 12:33 birth_info
-rw-rw-r--. 1 eleiss eleiss       76 Sep 15 11:42 cities
drwxr-xr-x. 2 eleiss eleiss     4096 Jun 30 17:33 Desktop
drwxr-xr-x. 2 eleiss eleiss     4096 Apr 25 00:36 Documents
drwxr-xr-x. 2 eleiss eleiss     4096 Mar 29 21:19 Downloads
drwxrwxr-x. 3 eleiss eleiss     4096 Sep  4 19:46 empty
[eleiss@eleiss ~]$ ls -l | tail -n +2 | tr -s " "
-rw-------. 1 eleiss eleiss 14653 Feb 3 2011 autosave.xmi
drwxr-xr-x. 2 eleiss eleiss 4096 Sep 29 2010 bin
-rw-rw-r--. 1 eleiss eleiss 80 Sep 15 12:33 birth_info
-rw-rw-r--. 1 eleiss eleiss 76 Sep 15 11:42 cities
drwxr-xr-x. 2 eleiss eleiss 4096 Jun 30 17:33 Desktop
drwxr-xr-x. 2 eleiss eleiss 4096 Apr 25 00:36 Documents
drwxr-xr-x. 2 eleiss eleiss 4096 Mar 29 21:19 Downloads
drwxrwxr-x. 3 eleiss eleiss 4096 Sep 4 19:46 empty
[eleiss@eleiss ~]$ ls -l | tail -n +2 | tr -s " " | cut -d" " -f 1,9
-rw-------. autosave.xmi
drwxr-xr-x. bin
-rw-rw-r--. birth_info
-rw-rw-r--. cities
drwxr-xr-x. Desktop
drwxr-xr-x. Documents
drwxr-xr-x. Downloads
drwxrwxr-x. empty
[eleiss@eleiss ~]$

The output of ls -l is a formatted one. There are uncertain number of spaces between two fields in each row.

In the second command tr squeezes all the extra spaces from the output.

Using the squeezed output from tr command, the permission (first field) and the name (ninth field) of each files in the current directory can be derived easily using cut command.


You may write to resources@eleiss.com for any Linux related queries.

advertisement