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

Analysis of the use of trap commands in Linux

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "case analysis of the use of trap commands in Linux". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

By using tr, you can easily implement many of the most basic functions of sed. You can think of tr as a (extremely) simplified variant of sed: it can replace one character with another, or it can remove some characters altogether. You can also use it to remove duplicate characters. This is what all tr can do.

Syntax tr`` [OPTION]... SET1 [SET2] parameter

3.1-s compress repeating characters

-s: squeeze-repeats, replacing the corresponding repeating character (replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character) with the character specified by SET1

Xiaosi@Qunar:~ ``/ test`` $``echo`` "aaabbbaacccfddd"`` | ``tr``-s [abcdf] ``/ /`` abacfd

You can use this feature to delete blank lines in the file, essentially the same as above, replacing the corresponding repeating characters with the characters specified by SET1.

Xiaosi@Qunar:~/test$ cat b.txtI like footballFootball is very funnel Helloxiaosiao Qunardisuxian cat b.txt cat b.txt | tr-s ["\ n"] I like footballFootball is very funnel Hello

3.2-d Delete characters

-d:delete, deletes all characters specified in SET1, and does not convert (delete characters in SET1, do not translate)

Xiaosi@Qunar:~/test$ echo "a12HJ13fdaADff" | tr-d "[a Muyz] [Amurz]" 1213xiaosiao Qunarlu Greater Test$ echo "a1213fdasf" | tr-d [adfs] 1213

3.3 character replacement

-t:truncate, which replaces the characters in SET1 with the characters corresponding to SET2. The default is-t.

Xiaosi@Qunar:~/test$ echo "a1213fdasf" | tr-t [afd] [AFO] / / A1213FOAsF

The above code converts a to A, F to F, d to O.

We can make use of this feature to realize the conversion between big and small letters.

Xiaosi@Qunar:~/test$ echo "Hello World I Love You" | tr-t [Amurz] [Amurz] HELLO WORLD I LOVE YOUxiaosi@Qunar:~/test$ echo "HELLO WORLD I LOVE YOU" | tr-t [Amurz] [Amurz] hello world i love you

You can also use character sets for conversion.

Xiaosi`` @ test$ echo ``: ~ / test$ echo`` "Hello World I Love You" ``| tr-t [: lower:] [: upper:] ``HELLO WORLD I LOVE YOU`xiaosi`` @ Qunar``: ~ / test$ echo`` "HELLO WORLD I LOVE YOU" ``| tr-t [: upper:] [: lower:] ``hello world i love you

Note:

The set of characters is as follows

\ NNN octal character NNN (1 to 3 is octal value character)\\ backslash\ a Ctrl-G ringtone\ b Ctrl-H backspace\ f Ctrl-L walk page feed\ nCtrl-J new line\ r Ctrl-M enter\ t Ctrl-I tab key\ v Ctrl-X horizontal tab CHAR1-CHAR2 all characters from CHAR1 to CHAR2 [CHAR*] in SET2, copies of CHAR until length of SET1 [CHAR*REPEAT] REPEAT copies of CHAR REPEAT octal if starting with 0 [: alnum:] all letters and numbers [: alpha:] all letters [: blank:] horizontal tabs Blank, etc. [: cntrl:] all control characters [: digit:] all digits [: graph:] all printable characters, excluding spaces [: lower:] all lowercase characters [: print:] all printable characters, including spaces [: punct:] all punctuation characters [: space:] all horizontal or vertical white space [: upper:] all uppercase letters

3.4 character complement replacement

-c:complement, replacing characters not included in SET1 with SET2

Xiaosi@Qunar:~/test$ cat a.txtMonday 09:00Tuesday 09:10Wednesday 10:11Thursday 11:30Friday 08:00Saturday 07:40Sunday 10 tr 00xiaosiao Qunarlun xiaosi@Qunar:~/test$ cat a.txtMonday 09:00Tuesday 09:10Wednesday 10:11Thursday 11:30Friday 08:00Saturday 07:40Sunday $cat a.txt | tr-t "#"\ n" MondayTuesdayWednesdayThursdayFridaySaturdaySunday |

Tr-c "a murz"#" in the above code means to replace all characters except big and small letters with #.

The above code can be optimized to:

Xiaosi@Qunar:~/test$ cat a.txt | tr-cs "[Amurz] [Amurz]"\ n" MondayTuesdayWednesdayThursdayFridaySaturdaySunday "Linux trap command using instance analysis" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report