Showing posts with label Translate/ tr Command. Show all posts
Showing posts with label Translate/ tr Command. Show all posts

Tuesday, 17 November 2020

tr command in Unix/Linux with examples

LPI Tutorial and Material, LPI Guides, LPI Certifications, LPI Learning

The tr command in UNIX is a command line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace. It can be used with UNIX pipes to support more complex translation. tr stands for translate.

Syntax :


$ tr [OPTION] SET1 [SET2]

Options


-c : complements the set of characters in string.i.e., operations apply to characters not in the given set
-d : delete characters in the first set from the output.
-s : replaces repeated characters listed in the set1 with single occurrence
-t : truncates set1

Sample Commands


1. How to convert lower case to upper case


To convert from lower case to upper case the predefined sets in tr can be used.

$cat lpifile

Output:

WELCOME TO
LPICentral

$cat lpifile | tr “[a-z]” “[A-Z]”

Output:

WELCOME TO
LPICentral

or

$cat lpifile | tr “[:lower:]” “[:upper:]”

Output:

WELCOME TO
LPICENTRAL

2. How to translate white-space to tabs


The following command will translate all the white-space to tabs

$ echo "Welcome To LPICentral" | tr [:space:] '\t'

Output:

Welcome    To    LPICentral  

3. How to translate braces into parenthesis


You can also translate from and to a file. In this example we will translate braces in a file with parenthesis.

$cat lpifile

Output:

 {WELCOME TO}
LPICentral

$ tr '{}' '()'   newfile.txt

Output:

(WELCOME TO)
LPICentral

The above command will read each character from “lpifile.txt”, translate if it is a brace, and write the output in “newfile.txt”.

4. How to use squeeze repetition of characters using -s


To squeeze repeat occurrences of characters specified in a set use the -s option. This removes repeated instances of a character.

OR we can say that,you can convert multiple continuous spaces with a single space

$ echo "Welcome    To    LPICentral" | tr -s [:space:] ' '

Output:

Welcome To LPICentral

5. How to delete specified characters using -d option


LPI Tutorial and Material, LPI Guides, LPI Certifications, LPI Learning
To delete specific characters use the -d option.This option deletes characters in the first set specified.

$ echo "Welcome To LPICentral" | tr -d 'w'

Output:

elcome To LPICentral

6. To remove all the digits from the string, use


$ echo "my ID is 73535" | tr -d [:digit:]

Output:

my ID is

7. How to complement the sets using -c option


You can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.

$ echo "my ID is 73535" | tr -cd [:digit:]

Output:

73535

Saturday, 9 November 2019

Translate/ tr Command Examples in Unix and Linux

Translate/ tr Command, LPI Study Materials, LPI Guides, LPI Tutorial and Materials

Tr stands for translate or transliterate. The tr utility in unix or linux system is used to translate, delete or squeeze characters. The syntax of tr command is

tr [options] set1 [set2]

The options of tr command are:
◒ -c : complements the set of characters in string.
◒ -d : deletes the characters in set1
◒ -s : replaces repeated characters listed in the set1 with single occurrence
◒ -t : truncates set1

Tr command Examples:

1. Convert lower case letters to upper case


The following tr command translates the lower case letters to capital letters in the give string:

> echo "linux dedicated server" | tr "[:lower:]" "[:upper:]"
LINUX DEDICATED SERVER
> echo "linux dedicated server" | tr "[a-z]" "[A-Z]"
LINUX DEDICATED SERVER

2. Transform upper case letters to lower case.


Similar to the above example, you can translate the uppercase letters to small letters.

> echo "UNIX DEDICATED SERVER" | tr "[:upper:]" "[:lower:]"
unix dedicated server
> echo "UNIX DEDICATED SERVER" | tr "[A-Z]" "[a-z]"
unix dedicated server

3. Replace non-matching characters.


The -c option is used to replace the non-matching characters with another set of characters.

> echo "unix" | tr -c "u" "a"
uaaa

Translate/ tr Command, Unix Command, Linux Command

In the above example, except the character "u" other characters are replaced with "a"

4. Delete non-printable characters


The -d option can be used to delete characters. The following example deletes all the non-printable characters from a file.

> tr -cd "[:print:]" < filename


5. Squeezing characters


You can squeeze more than one occurrence of continuous characters with single occurrence. The following example squeezes two or more successive blank spaces into a single space.

> echo "linux    server" | tr -s " "
linux server

Here you can replace the space character with any other character by specifying in set2.

> "linux    server" | tr -s " " ","
linux,server

6. Delete characters


The following example removes the word linux from the string.

> echo "linuxserver" | tr -d "linux"
server

Saturday, 6 April 2019

tr command in Unix/Linux with examples

LPI Tutorial and Material, LPI Guides, LPI Certifications, LPI Learning

The tr command in UNIX is a command line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace. It can be used with UNIX pipes to support more complex translation. tr stands for translate.

Syntax :


$ tr [OPTION] SET1 [SET2]

Options


-c : complements the set of characters in string.i.e., operations apply to characters not in the given set
-d : delete characters in the first set from the output.
-s : replaces repeated characters listed in the set1 with single occurrence
-t : truncates set1

Sample Commands


1. How to convert lower case to upper case


To convert from lower case to upper case the predefined sets in tr can be used.

$cat greekfile

Output:

WELCOME TO
LPICentral

$cat greekfile | tr “[a-z]” “[A-Z]”

Output:

WELCOME TO
LPICentral

or

$cat lpifile | tr “[:lower:]” “[:upper:]”

Output:

WELCOME TO
LPICENTRAL

2. How to translate white-space to tabs


The following command will translate all the white-space to tabs

$ echo "Welcome To LPICentral" | tr [:space:] '\t'

Output:

Welcome    To    LPICentral 

3. How to translate braces into parenthesis


You can also translate from and to a file. In this example we will translate braces in a file with parenthesis.

$cat greekfile

Output:

 {WELCOME TO}
LPICentral
$ tr '{}' '()'   newfile.txt

Output:

(WELCOME TO)
LPICentral

The above command will read each character from “lpifile.txt”, translate if it is a brace, and write the output in “newfile.txt”.

4. How to use squeeze repetition of characters using -s


To squeeze repeat occurrences of characters specified in a set use the -s option. This removes repeated instances of a character.
OR we can say that,you can convert multiple continuous spaces with a single space

$ echo "Welcome    To    LPICentral" | tr -s [:space:] ' '

Output:

Welcome To LPICentral

5. How to delete specified characters using -d option


To delete specific characters use the -d option.This option deletes characters in the first set specified.

$ echo "Welcome To LPICentral" | tr -d 'w'

Output:

elcome To LPICentral

6. To remove all the digits from the string, use


$ echo "my ID is 73535" | tr -d [:digit:]

Output:

my ID is


7. How to complement the sets using -c option


You can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.

$ echo "my ID is 73535" | tr -cd [:digit:]

Output:

73535

Or

$ echo “unix” | tr –c “u” “a”

Output:

Uaaa

Wednesday, 9 May 2018

Translate/ tr Command Examples in Unix and Linux

Translate/ tr Command, Unix Command, Linux Command

Tr stands for translate or transliterate. The tr utility in unix or linux system is used to translate, delete or squeeze characters. The syntax of tr command is

tr [options] set1 [set2]

The options of tr command are:
◒ -c : complements the set of characters in string.
◒ -d : deletes the characters in set1
◒ -s : replaces repeated characters listed in the set1 with single occurrence
◒ -t : truncates set1

Tr command Examples:

1. Convert lower case letters to upper case


The following tr command translates the lower case letters to capital letters in the give string:

> echo "linux dedicated server" | tr "[:lower:]" "[:upper:]"
LINUX DEDICATED SERVER
> echo "linux dedicated server" | tr "[a-z]" "[A-Z]"
LINUX DEDICATED SERVER

2. Transform upper case letters to lower case.


Similar to the above example, you can translate the uppercase letters to small letters.

> echo "UNIX DEDICATED SERVER" | tr "[:upper:]" "[:lower:]"
unix dedicated server
> echo "UNIX DEDICATED SERVER" | tr "[A-Z]" "[a-z]"
unix dedicated server

3. Replace non-matching characters.


The -c option is used to replace the non-matching characters with another set of characters.

> echo "unix" | tr -c "u" "a"
uaaa

Translate/ tr Command, Unix Command, Linux Command

In the above example, except the character "u" other characters are replaced with "a"

4. Delete non-printable characters


The -d option can be used to delete characters. The following example deletes all the non-printable characters from a file.

> tr -cd "[:print:]" < filename


5. Squeezing characters


You can squeeze more than one occurrence of continuous characters with single occurrence. The following example squeezes two or more successive blank spaces into a single space.

> echo "linux    server" | tr -s " "
linux server

Here you can replace the space character with any other character by specifying in set2.

> "linux    server" | tr -s " " ","
linux,server

6. Delete characters


The following example removes the word linux from the string.

> echo "linuxserver" | tr -d "linux"
server