Shell Script Learning Guide Text Processing tool _linux Shell

Source: Internet
Author: User
Tags locale

4.1 Sort Text

Sort of 4.1.1 Rows
When command-line options are not provided, the entire record is sorted according to the order defined by the current locale.
In the traditional C locale, which is the ASCII order.

4.1.2 Sorted by Field
The-K option is followed by a field number, or a pair of digits.
Each number can be followed by the character position of a point number, or modifier letters.

If you specify only one field number, the sort key value starts at the beginning of the field.
Continue to the end of the record (not the end of the field).

If you give a pair of field numbers separated by commas, the sort key value starts at the beginning of the first field value.
Ends at the end of the second field value. Use the dot number to indicate the character position.
-k2.4, 5.6 refers to the beginning of the second field from the fourth character comparison, has been compared to the fifth field of the sixth character.
$ sort-t:-k1,1/etc/passwd sorted by user name
Bin:x:1:1:bin:/bin:/sbin/nologin
Chico:x:12501:1000:chico Marx:/home/chico:/bin/bash
Harpo:x:12502:1000:harpo Marx:/home/harpo:/bin/ksh
...

$ sort-t:-k3,3nr/etc/passwd introspection uid Sort
or-k3nr,3 or-k3,3-n-R can be.

4.1.3 The sorting of text blocks
Sometimes you need to sort the data in a combination of multiline records. Take the address list for example:
$ cat My-friends
# Sortkey:schlo, Hans Jurgen
Hans Jurgen Schlo
Unter den Linden 78
D-10117 Berlin
Germany

# Sortkey:jones, Adrian
...

The trick is to use awk to identify the paragraph spacing and temporarily replace the branch with an unused character within each address.
The line you see in sort becomes this:
# Sortkey:schlo, Hans jurgen^zhans Jurgen schlo^zunter den Linden 78^z ...
Cat My-friends | Read Address file
Awk-v rs= "" ' {gsub ("\ n", "^z");    Print} ' | Convert address to Single line
Sort-f | Sort address data, ignoring case
Awk-v ors= "\ n" ' {gsub ("^z", "\ n"); print} ' | Restore row structure
Grep-v ' # SORTKEY ' delete tag line
1. Function Gsub () is a global replacement (global substitution), similar to the s/x/y/g structure under SED.
2. The recording separator (record Separator) for entering data when the RS variable is entered.
You typically enter data to separate lines so that each row becomes a single record.
Rs= "" is a special use, meaning that the record is separated by a blank line.
3. ORS is the output recorder separator.
Note: ' {action} ' is an action on each field, and Rs,ors is the setting for the record.

Stability of 4.1.5 Sort
Sort is not stable.

4.2 Delete Duplicates
Sort-u is based on matching key values for elimination operations, not matching records.
Uniq has 3 handy options:
-C plus the number of times the row repeats before each output line.
-D is used to display only duplicate rows.
-U displays only rows that are not duplicates.

4.3 Reformatting a paragraph
Fwt-w 30

4.4 Count the number of lines, words, and characters
The default output for WC is a row of reports, including the number of lines, words, and bytes.
Available options:-C (bytes),-l (lines),-W (word number).

Copy Code code as follows:

$ echo Testing One Two three | Wc-c
1 4 22
$ wc/etc/passwd/etc/group


4.6 Extraction of the beginning or end lines
Displays the top N records for each of the file list:
Head-n n [File (s)]
head-n [File (s)]
awk ' FNR <= n ' [File (s)]
SED-E NQ [File (s)]
sed NQ [File (s)]

Observe the growing System Information log, ctrl-c stop tail.

Copy Code code as follows:

$ tail-n 25-f/var/log/messages

Related Article

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.