Linux Text Processing 1

Source: Internet
Author: User

Linux Text Processing 1

Everything in linux is a file, so it is very important to process the text. The following describes several common text processing commands:

Cat

The basic usage of cat is as follows:

We created the cattest.txt file. The beginning of the text is a tab, and the first line has a line break. Common cat options are as follows:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KCi1BINGhz + 7P1Mq + release/2NbG19a3 + release/skLW4gINGhz9/OqsO/release/2 NbGt/release = "http://www.2cto.com/uploadfile/Collfiles/20150407/20150407093618868.jpg" alt = "\">
Note: The ^ K in the above table can be implemented by pressing Ctrl + K. For ^ M: DOS (windows System), replace behavior ^ M ^ J (carriage return + line feed) is called CR and LF. in linux, only LF (^ J/line feed) is used) this line feed symbol. When you use ssh to pass files under windows to the server, a ^ M will appear at the end of each line in the file, which is an error during file operations. Therefore, delete ^ M, there are two methods: convert a windows file to a unix file, that is, the dos2unix file directly uses a text command to delete ^ M
SortThe sort program sorts the content of a specified file or multiple files after the command line, and then sends the sorting result to the standard output.
The general sort options are as follows:

Option Description
-B Generally, the sorting of sort starts with the first character of each line,
-Option B sorts sort from the first non-blank character
-F Case Insensitive
-N Sort strings by numeric values.
-R Reverse Selection
-O Specify the output file
-T Select a field separator. By default, the field separator is a space or tab.
-K filed1 [, field2] Sorts not all characters from field1 to field2.
Sort:
Sort by a field. The sort command generally uses spaces or tabs as the default delimiter (the number of spaces between two identical fields in each line is not necessarily the same, do not use spaces between multiple fields in a row)

-K 5 indicates that the fifth field is used for sorting. the starting position of the field is 1. In fact, sorting a field is the sorting of all characters from the current field to the end of the line.
From the fifth field to the end of the line. Sort also allows you to sort multiple fields separately, so you can specify multiple sorting fields.
When the first key is selected, we specify a region. 1, 1 indicates that the first field starts and ends with the first field. In the second field, we specify 2n, which means that the fields are sorted by numbers. You can add option letters after the field position for sorting, such as B (ignore spaces at the beginning), n (numerical sorting), and r (reverse sorting.
The date format contained in the third field in our list is not conducive to sorting. So how can we sort the list in chronological order? Sort can specify the offset in the key.

Specify-k 3.1,-k 3.4, and-k 3.7 to specify the month, day, and year as the sort key value. We can see that the results of the same sorting field and different writing order are different, and the leftmost sorting key value has a high weight.

In a file, not all file field keys are separated by spaces or tabs. Therefore, you need to specify separators by yourself.
Sort uses the-t option to specify the delimiter.

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.