Linux tr Command
The Linux tr command is used to translate or delete characters in a file.
The tr command reads data from the standard input device, translates the character strings, and outputs the results to the standard output device.
Syntax
tr [-cdst][--help][--version][FIRST_SET][SECOND_SET]
tr [OPTION]…SET1[SET2]
Parameter Description:
-c, --complement: Complement the set of characters in SET1. Only the remaining characters that do not match SET1 are translated.
-d, --delete: Delete specified characters.
-s, --squeeze-repeats: Reduce consecutive repeated characters to a single character.
-t, --truncate-set1: Truncate SET1 to the length of SET2.
--help: Display usage information.
--version: Display the version information of the program.
Character set ranges:
\NNN: Character with octal value NNN (1 to 3 octal digits)
\: Backslash
\a: Ctrl-G bell
\b: Ctrl-H backspace
\f: Ctrl-L form feed
\n: Ctrl-J new line
\r: Ctrl-M carriage return
\t: Ctrl-I tab
\v: Ctrl-X horizontal tab
CHAR1-CHAR2: Specifies a range of characters from CHAR1 to CHAR2, based on ASCII order, only ascending.
[CHAR*]: A SET2-specific setting that repeats the specified character until it matches the length of SET1.
[CHAR*REPEAT]: A SET2-specific setting that repeats the specified character until the set REPEAT count (REPEAT count is in octal, starting from 0).
[:alnum:]: All alphanumeric characters
[:alpha:]: All alphabetic characters
[:blank:]: All horizontal spaces
[:cntrl:]: All control characters
[:digit:]: All digits
[:graph:]: All printable characters (excluding spaces)
[:lower:]: All lowercase letters
[:print:]: All printable characters (including spaces)
[:punct:]: All punctuation characters
[:space:]: All horizontal and vertical spaces
[:upper:]: All uppercase letters
[:xdigit:]: All hexadecimal digits
[=CHAR=]: All characters matching the specified character (CHAR within the equals sign can be customized).
Example
To convert all lowercase letters to uppercase in the file testfile, you can use the following command:
cat testfile | tr a-z A-Z
The content of the testfile is as follows:
$ cat testfile # Original content of testfile
Linux networks are becoming more and more common,
but security is often an overlooked
issue. Unfortunately, in today’s environment all networks
are potential hacker targets,
from tp-secret military research networks to small home LANs.
Linux Network Security focuses on securing Linux in a
networked environment, where the
security of the entire network needs to be considered
rather than just isolated machines.
It uses a mix of theory and practical techniques to
teach administrators how to install and
use security applications, as well as how the
applications work and why they are necessary.
After using the tr command for case conversion, the output is as follows:
$ cat testfile | tr a-z A-Z # Converted output
LINUX NETWORKS ARE BECOMING MORE AND MORE COMMON, BUT SECURITY IS OFTEN AN OVERLOOKED
ISSUE. UNFORTUNATELY, IN TODAY’S ENVIRONMENT ALL NETWORKS ARE POTENTIAL HACKER TARGETS,
FROM TP-SECRET MILITARY RESEARCH NETWORKS TO SMALL HOME LANS.
LINUX NETWORK SECURITY FOCUSES ON SECURING LINUX IN A NETWORKED ENVIRONMENT, WHERE THE
SECURITY OF THE ENTIRE NETWORK NEEDS TO BE CONSIDERED RATHER THAN JUST ISOLATED MACHINES.
IT USES A MIX OF THEORY AND PRACTICAL TECHNIQUES TO TEACH ADMINISTRATORS HOW TO INSTALL AND
USE SECURITY APPLICATIONS, AS WELL AS HOW THE APPLICATIONS WORK AND WHY THEY ARE NECESSARY.
ISSUE. UNFORTUNATELY, IN TODAY'S ENVIRONMENT ALL NETWORKS ARE POTENTIAL HACKER TARGETS, FROM TP-SECRET MILITARY RESEARCH NETWORKS TO SMALL HOME LANS. LINUX NETWORK SECURITY FOCUSES ON SECURING LINUX IN A NETWORKED ENVIRONMENT, WHERE THE SECURITY OF THE ENTIRE NETWORK NEEDS TO BE CONSIDERED RATHER THAN JUST ISOLATED MACHINES. IT USES A MIX OF THEORY AND PRACTICAL TECHNIQUES TO TEACH ADMINISTRATORS HOW TO INSTALL AND USE SECURITY APPLICATIONS, AS WELL AS HOW THE APPLICATIONS WORK AND WHY THEY ARE NECESSARY.