Linux tr command
The Linux tr command is used to convert 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: Reverse the set of selected characters. That is, the part matching SET1 is left unprocessed, and the remaining non-matching part is converted.
- -d, --delete: Delete characters in the specified set.
- -s, --squeeze-repeats: Reduce consecutive repeated characters to a single specified character.
- -t, --truncate-set1: Trim the SET1 range so that its length equals the SET2 length.
- --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 New line
- \r Ctrl-M Carriage return
- \t Ctrl-I Tab key
- \v Ctrl-X Horizontal tab
- CHAR1-CHAR2 Character range from CHAR1 to CHAR2, based on ASCII code order, can only be from small to large, not from large to small.
- [CHAR*]: This is a SET2-specific setting that repeats the specified character until it reaches the same length as SET1.
- [CHAR*REPEAT] This is also a SET2-specific setting that repeats the specified character for the specified number of REPEAT times (the REPEAT number is calculated in octal, starting from 0).
- [:alnum:]: All alphabetic characters and digits
- [: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 in the equals signs represents a custom character you can define)
Examples
To convert all lowercase letters in the file testfile to uppercase, use the following command:
cat testfile |tr a-z A-Z
The contents of the testfile file 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 using the tr command for case conversion, the following output result 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 result 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.Other Extensions
Linux Command Reference