Five character conversion commands in Linux: TR, Col, join, paste, expand

Source: Internet
Author: User

Common character conversion commands in Linux include TR, Col, join, paste, expand.

(1) TR: Can Be Used to delete text in a piece of information or convert text information.

A) The default operation of TR 'set1' 'set2' TR is to replace the characters in set1 contained in the standard input with the corresponding characters in set2.

Example: Echo "Hello, Kevin" | tr 'a-z'' A-Z'

Output result: Hello, Kevin

Example: Echo "Hello, Kevin" | tr 'he ''xx'

The output result is hxllo and kxvin.

B) tr-D set1 when Tr has the-D parameter, it means to delete all characters in set1 from the standard input.

Example: Echo "Hello, Kevin" | tr-D 'a-Z'

The output result is H. K is used to delete all lowercase letters in the standard input.

C) tr-s set1 when Tr has the-S parameter, it indicates that only one character is retained in set1 in the standard input.

Example: Echo "Hello, Kevin" | tr-s 'a-Z'

The output result is Helo. Only one L is retained when the duplicate L is deleted in the Kevin result.

(2) COL: Many Unix instruction files contain RLF control characters. When we use shell special characters ">" and ">" to output the content of the description file into a plain text file, the control characters will become garbled characters, the Col command can effectively filter out these control characters.

A) col-B filters out all control characters, including RLF and hrlf.

B) col-x Replace the tab with the equivalent space key.

(3) join: It processes data between two files, mainly to merge the rows with the same data in the two files. Note that before using join, the two files to be processed should be sorted; otherwise, some items to be compared will be skipped.

-T: delimiter used for segmentation. By default, join separates data with space characters and compares the data of the "first field". If the two files are the same, join the two data lines and place the first field in the first one.

-I: Case sensitivity differences are ignored.

-1: digit 1 represents the field used for analysis in the first file.

-2: digit 2 represents the field used for analysis in the second file.

For example, test1 contains the following data:

Kevin 100Luna 100Max 100John 100

Test2 contains the following data:

Luna 10John 20Max 30Kevin 40

Sort test1 and Test2 respectively and save them in test1_s and test2_s, and then use join to process the two files: Join test1 Test2 to obtain the following results:

John 120Kevin 140Luna 110Max 130

(4) paste: directly paste the rows with the same data in the two files together and separate them with tabs.

-D: It can be followed by a separator, which is separated by tab by default.

-: If the file is partially written as-, the data is from stdin.

Or use paste to process the sorted test1_s and test2_s following the example above. paste-D ''test1_s test2_s will get the following results:

John 100 John 20Kevin 100 Kevin 40Luna 100 Luna 10Max 100 Max 30

(5) Expand: convert a tab to a space key.

-T: numbers can be followed. You can customize the number of characters that a tab button represents.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.