English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux tr command

Linux Command大全

The Linux tr command is used to convert or delete characters in a file.

The 'tr' command reads data from the standard input device, translates the string, and then outputs the result to the standard output device.

syntax

tr [-cdst][--help][--version][First character set][Second character set]  
tr [OPTION]…SET1[SET2]

Parameter description:

  • -c, --complement: Invert the set of characters. That is, match SET1 part is not processed, and the remaining part that does not match is converted

  • -d, --delete: Delete instruction character

  • -s, --squeeze-repeats: Reduce consecutive repeated characters to the specified single character

  • -t, --truncate-set1: Reduce SET1 Specify the range so that it matches SET2 Set the length to be equal

  • --help: Display program usage information

  • --version: Display the version information of the program itself

Character set range:

  • \NNN An octal value character NNN (1 to 3 as an octal value character)

  • \\ Backslash

  • \a Ctrl-G Bell

  • \b Ctrl-H Backspace

  • \f Ctrl-L Line feed

  • \n Ctrl-J New line

  • \r Ctrl-M Carriage return

  • \t Ctrl-I Tab key

  • \v Ctrl-X Horizontal tab

  • CHAR1-CHAR2 : The character range starts from CHAR1 to CHAR2 specification, the specification of the range is based on the order of ASCII code, and can only increase from small to large, not from large to small.

  • [CHAR*] : This is SET2 A dedicated setting, the function is to repeat the specified character to the length specified by SET1 to the same length

  • [CHAR*REPEAT] : This is also SET2 A dedicated setting, the function is to repeat the specified character to the set REPEAT times (the number of REPEAT is 8 Decimal system calculation, starting with 0)

  • [:alnum:] : All letter characters and numbers

  • [:alpha:] : All letter characters

  • [:blank:] : All horizontal spaces

  • [:cntrl:] : All control characters

  • [:digit:] : All numbers

  • [:graph:] : All printable characters (excluding space characters)

  • [:lower:] : All lowercase letters

  • [:print:] : All printable characters (including space characters)

  • [:punct:] : All punctuation characters

  • [:space:] : All horizontal and vertical space characters

  • [:upper:] : All uppercase letters

  • [:xdigit:] : All 16 Decimal number system

  • [=CHAR=] : All characters that match the specified character (CHAR in the equal sign, representing the customizable character you can set)

Online example

Convert all lowercase letters in the file testfile to uppercase, you can use the following command at this time:

cat testfile | tr a-z A-Z

THE CONTENT OF testfile IS AS FOLLOWS:

$ cat testfile # THE 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-RANGE FROM SECRET MILITARY RESEARCH NETWORKS TO SMALL HOME LANs.  
LINUX NETWORK SECURITY FOCUSES ON SECURING LINUX IN A 
NETWORKED ENVIRONMENT, WHERE THE  
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.

THE OUTPUT AFTER USING THE tr COMMAND FOR CASE CONVERSION IS AS FOLLOWS:

$ cat testfile | tr a-z A-Z # THE OUTPUT AFTER THE TRANSFORMATION  
LINUX NETWORKS ARE BECOMING MORE AND MORE COMMON, BUT SECURITY IS OFTEN AN OVERLOOKED ISSUE  
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  
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 APPLICATIONS WORK AND WHY THEY ARE NECESSARY.

CASE CONVERSION CAN ALSO BE ACHIEVED THROUGH THE [:lower][:upper] PARAMETER. FOR EXAMPLE, USE THE FOLLOWING COMMAND:

cat testfile | tr [:lower:] [:upper:]

THE OUTPUT IS AS FOLLOWS:

$ cat testfile | tr [:lower:] [:upper:] # THE OUTPUT AFTER THE TRANSFORMATION  
LINUX NETWORKS ARE BECOMING MORE AND MORE COMMON, BUT SECURITY IS OFTEN AN OVERLOOKED ISSUE  
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  
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 APPLICATIONS WORK AND WHY THEY ARE NECESSARY.

Linux Command大全