Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use tr command in Linux system

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use the tr command in the Linux system", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to use the tr command in the Linux system" this article.

Tr conversion, compression, truncation, deletion of characters

Tr [option]. Set1 [set2]

Parameters:

-DMAE, Musi, delete

Delete the characters in string 1 without conversion.

-Scrimson, squeeze, repeats.

Compresses a recurring string into a single string.

-tmam, Musi, truncate, set1.

Truncate the length of string 1 to the length of string 2.

-cmam Mustang

Take the complement of string 1. Requires the character set to be ASCII.

-- help printing help.

-- version displays moderator information.

The escape characters are as follows:

Octal code of\ NNN character

\\ backslash

\ a BELL ringing

\ b spaces

\ c do not print new line characters

\ nNew line

\ r enter

\ t horizontal skipping

\ v firm jump box

Character mode:

CHAR1-CHAR2 means from the characters CHAR1 to CHAR2. For example, a Meli ZJ, a mai Z.

[CHAR*] represents one or several consecutive CHAR.

[CHAR*REPEAT] represents the number of REPEAT CHAR.

[: alnum:] represents all letters and numbers.

[: alpha:] indicates all letters.

[: blank:] represents horizontal spaces.

[: cntrl:] represents a control character.

[: digit:] represents all numbers.

[: graph:] represents printable characters, excluding spaces.

[: lower:] indicates lowercase letters.

[: upper:] indicates uppercase letters.

[: print:] represents printable characters, including spaces.

[: xdigit:] represents a hexadecimal number.

[= CHAR=] indicates CHAR

Example:

(1) remove repeated lowercase characters from oops.txt

The code is as follows:

Tr-s "[a Merz]" result.txt

(2) Delete blank lines

The code is as follows:

Tr-s "[012]"

< plan.txt 或 tr -s ["\n"] < plan.txt (3)有时需要删除文件中的^M,并代之以换行 代码如下: tr -s "[\015]" "[\n]" < file 或 tr -s "[\r]" "[\n]" < file (4)大写到小写 代码如下: cat a.txt |tr "[a-z]" "[A-Z]" >

B.txt

(5) Delete the specified character

A week's schedule. The task is to remove all numbers from it and keep only the date. Dates are in both uppercase and lowercase format. So you need to specify two character ranges [a murz] and [A murz], and command tr-cs "[a murz] [A murz]"[\ 012 *]" to place all strings in each line of the file that are not contained in [a murz] or [Amurz] (all Greek letters) in string 1 and convert them to a new line. The-s option indicates that all new lines are compressed, and-c indicates that all letters remain unchanged. The original file is as follows, followed by the tr command:

The code is as follows:

Tr-cs "[Amurz] [Amurz]" [\ 012*] "

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report