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

Introduction to the use of tr command

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

tr

translate or delete characters

I understand this to mean converting characters or deleting unwanted characters

tr [OPTION]... SET1 [SET2]

Convert characters from standard input from SET1 to SET2

Example: Convert all upper case to lower case

#echo "HELLO HOW ARE YOU? " | tr 'A-Z' 'a-z'

Note: This can only be used for stdin.

Common options:

-c: Complement set, the so-called complement set is all the characters except the given character. This option cannot be used alone.

-d: Delete specified character

-s: compresses specified duplicate characters

Examples of usage:

-d: For example, you can see that the specified collection has been deleted

#echo "Hello 1 how 2 are 3 you 4 ? " |tr -d '[0-9]' (brackets can be omitted)

-c: As you can see from the example, the specified set is all numbers, spaces, newlines, and because of the-d option, everything except the set is deleted.

#echo "Hello 1 how 2 are 3 you 4 ? " | tr -d -c '0-9 \n'

-s: As you can see from the example, the redundant spaces in the middle are compressed and only one is displayed

# echo "Hello 1 how 2 are 3 you 4 ? " | tr -s ' '

Definition set:

'A-Z' and 'a-z' are both sets, we can append characters or character classes as needed to construct sets

'Start character-end character': the way in which the meaning of a continuous set must be continuous, if not continuous, it is defined as 3 single elements, i.e. the start character,'-','end character';

Special characters: '\t' for tabs,'\n' for newlines

Character class:

alnum: all letters and numbers

digit: all numbers

alpha: all letters

space: white space character

upper: all upper case characters

lower: all lowercase characters

punct: punctuation

xdigit: hexadecimal character

How to use:

tr [: Character class:] [: Character class:]

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

Network Security

Wechat

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

12
Report