Use of Linux tr command

Use of Linux tr command

1. Introduction

tr is used to convert or delete a piece of text. tr is the abbreviation of translate, and its function in English is: translate or delete characters. All the functions of tr can be completed by sed, and tr can be regarded as a minimalist implementation of sed.

2. Format

tr [OPTION]... SET1 [SET2]

3. Options

-c, -C, --complement: Delete characters other than character set <character set1> or convert them to the last character in character set <character set2> (if you specify multiple characters). See Example 6.
-d, --delete: Delete the string SET1 in the information.
-s, --squeeze-repeats: Squeeze repeated characters and keep only one.
--help: Display help information.
--version: Display version information.

4. Examples

(1) Change all lowercase characters in the information output by last to uppercase characters.

last|tr '[az]' '[AZ]'
//or last|tr [az] [AZ]

(2) Delete the colon ":" from the information output by /etc/passwd.

cat /etc/passwd | tr -d ':'

(3) Convert DOS files into Unix files.

cat /etc/passwd | tr -d '\r'

(4) Delete blank lines

cat file | tr -s "\n" > new_file

(5) Replace "abc" that appears in the file with "xyz".

cat file | tr "abc" "xyz" > new_file

Note: Here, all occurrences of the letter "a" in the file are replaced with the letter "x", the letter "b" is replaced with the letter "y", and the letter "c" is replaced with the letter "z", rather than replacing the string "abc" with the string "xyz".

(6) Delete and replace characters outside the specified character set.

//Replace characters outside the specified character set [b3335@MIC ~]$ echo alv blv|tr -c 'lv ' "x"
xlv xlv

//Delete characters outside the specified character set [b3335@MIC ~]$ echo alv blv|tr -cd 'lv'
lvlv

The above is the detailed content of the use of Linux tr command. For more information about Linux tr command, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed Tutorial on Using xargs Command on Linux
  • Use of Linux ipcs command
  • Linux sar command usage and code example analysis
  • Use of Linux ls command
  • Use of Linux sed command
  • Use of Linux read command
  • Use of Linux usermod command
  • Use of Linux passwd command
  • Detailed explanation of the use of Linux time command
  • Use of Linux ln command
  • Use of Linux telnet command

<<:  WeChat applet to determine whether the mobile phone number is legal example code

>>:  Summarize several common ranking problems in MySQL

Recommend

MySQL data insertion efficiency comparison

When inserting data, I found that I had never con...

The process of using vxe-table to make editable tables in vue

There is a table in the project that needs to be ...

Let's talk in depth about the principle and implementation of new in JS

Table of contents definition Constructor bodies a...

How to execute PHP scheduled tasks in CentOS7

Preface This article mainly introduces the releva...

CentOS 6 uses Docker to deploy Zookeeper operation example

This article describes how to use docker to deplo...

How to open the port in Centos7

The default firewall of CentOS7 is not iptables, ...

Implementation of docker-compose deployment project based on MySQL8

1. First, create the corresponding folder accordi...

Advantages and disadvantages of Table layout and why it is not recommended

Disadvantages of Tables 1. Table takes up more byt...

Mysql get table comment field operation

I won't say much nonsense, let's just loo...

MySQL 8.0.12 decompression version installation tutorial personal test!

Mysql8.0.12 decompression version installation me...

Complete steps to install mysql5.7 on Mac (with pictures and text)

I recently used a Mac system and was preparing to...

centos 7 modify sshd | prohibit root login and sshd port script definition

1. Create a new user wwweee000 [root@localhost ~]...

How to implement batch deletion of large amounts of data in MySQL large tables

The question is referenced from: https://www.zhih...

HTML+CSS implementation code for rounded rectangle

I was bored and suddenly thought of the implementa...

MySQL partition table is classified by month

Table of contents Create a table View the databas...