Linux tr command
The Linux tr command is used to translate or delete characters in a file.
The tr command reads data from standard input, translates the string, and outputs the result to standard output.
Syntax
tr [-cdst][--help][--version][第一字符集][第二字符集]
tr [OPTION]…SET1[SET2]
Parameter Description:
- -c, --complement: Complement the set of characters. That is, the part matching SET1 is not processed, and only the remaining non-matching part is translated
- -d, --delete: Delete the specified characters
- -s, --squeeze-repeats: Reduce consecutive repeated characters to a single specified character
- -t, --truncate-set1: Truncate the range specified by SET1 to make it equal in length to SET2
- --help: Display program usage information
- --version: Display the version information of the program itself
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 newline
- \r Ctrl-M carriage return
- \t Ctrl-I tab
- \v Ctrl-X horizontal tab
- CHAR1-CHAR2: specifies a character range from CHAR1 to CHAR2. The range is based on ASCII code order and can only go from smaller to larger, not from larger to smaller.
- [CHAR*]: a setting specific to SET2; its function is to repeat the specified character until it reaches the same length as SET1
- [CHAR*REPEAT]: also a setting specific to SET2; its function is to repeat the specified character up to the set REPEAT count (the REPEAT number is computed in octal, starting from 0)
- [:alnum:]: all letters and digits
- [:alpha:]: all alphabetic characters
- [:blank:]: all horizontal whitespace
- [: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 whitespace characters
- [:upper:]: all uppercase letters
- [:xdigit:]: all hexadecimal digits
- [=CHAR=]: all characters matching the specified one (the CHAR between the equal signs represents a character you can customize)
Example
To convert all lowercase letters in the file testfile to uppercase, you can use the following command:
cat testfile |tr a-z A-Z
The contents of the file testfile are as follows:
$ cat testfile #testfile原来的内容
Linux networks are becoming more and more common,
but scurity is often an overlooked
issue. Unfortunately, in today’s environment all networks
are potential hacker targets,
fro0m tp-secret military research networks to small home LANs.
Linux Network Securty 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 practicl techniques to
teach administrators how to install and
use security applications, as well as how the
applcations work and why they are necesary.
After case conversion with the tr command, the following output is obtained:
$ cat testfile | tr a-z A-Z #转换后的输出
LINUX NETWORKS ARE BECOMING MORE AND MORE COMMON, BUT SCURITY 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 SECURTY 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 PRACTICL TECHNIQUES TO TEACH ADMINISTRATORS HOW TO INSTALL AND
USE SECURITY APPLICATIONS, AS WELL AS HOW THE APPLCATIONS WORK AND WHY THEY ARE NECESARY.
Case conversion can also be achieved with the [:lower][:upper] parameters. For example, use the following command:
cat testfile |tr [:lower:] [:upper:]
The output is as follows:
$ cat testfile | tr [:lower:] [:upper:] #转换后的输出
LINUX NETWORKS ARE BECOMING MORE AND MORE COMMON, BUT SCURITY 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 SECURTY 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 PRACTICL TECHNIQUES TO TEACH ADMINISTRATORS HOW TO INSTALL AND
USE SECURITY APPLICATIONS, AS WELL AS HOW THE APPLCATIONS WORK AND WHY THEY ARE NECESARY.
Linux Command Reference