CentOS Linux Server Guide

Replacing words and characters of a file  in CentOS Linux

tr - also known as 'Translate' for Linux or Unix. This is the command to find and replacin words and character for a given files

 

Command and Options:

tr  [options] string1 string2

 

 

Warning:

You must be extra careful when applying this command, all the redirection must be in the following

 

tr abc ABCD < A.log    (OK!)

 

However, you issue the wrong redirection command, you will destroy all the contents of the file

 

tr abc ABCD > A.log    (WRONG!)

 

 

Option:

 
Options   Description
-s  squeeze several character string into one
-d  remove specified character string
-c  complement the set of characters specified by string1
string 1  original characters of a file to be replaced
string 2  new characters to replace

 

Examples:

 

The the following example will replacing character of "abcd" with "ABCD"
tr abcd ABCD < mail.log

 

The following example will replacing all the lowercase characters (character to a-z) with Uppercase (character A to Z)
tr a-z A-z < A.log

 

The following example will replace any characters of a file  with "#" , EXCEPT number
tr -c 0-9 # < A.log

 

Back to Managing Files and Folders in CentOS Linux

 



Copyright 2011 http://www.1a-centosserver.com All Rights Reserved

All trademarks are the property of their respective owners.

Contact Us | Terms of Use | Privacy Policy