Source: Internet
Author: User
Tags sorts syslog

The sort command sorts the rows in the files specified by the file parameter and writes the results to the standard output. If the file parameter specifies multiple files, the sort command connects the files and sorts them as a file.

Sort syntax

[Root@www ~]# Sort [-fbmnrtuk] [file or stdin]
options and Parameters:-
F  : Ignores case differences, such as a and a as encoding;
-B  : Ignores the front spaces part; -M: Sort by the name of the  month, such as the sorting method of a few lines, DEC, and so on;-
n  : Sort using "pure numbers" (default is sorted by text type);
r  : reverse sort;  : Is Uniq, in the same data, only one row is represented;-
T  : separator, by default, by using the [tab] key to separate;
K  : The meaning of sorting by that interval (field)
Sort the accounts of/etc/passwd
[Root@www ~]# cat/etc/passwd | Sort
bin:x : 1:1:bin:/bin:/sbin/nologin

Sort is sorted by default with the first data, and by default it is sorted as a string, so the beginning of the letter A is sorted in ascending order.

/ETC/PASSWD content is: to separate, I want to sort by the third column, How to

[Root@www ~]# cat/etc/passwd | Sort-t ': '-K 3
games:x:12:100:games:/ Usr/games:/sbin/nologin

The default is to sort by string, if you want to use numeric sorting:

cat/etc/passwd | Sort-t ': '-K 3n
bin:x:2:2:bin :/bin:/bin/sh

The default is ascending sort, if you want to sort in reverse order, as follows

cat/etc/passwd | Sort-t ': '-K 3nr

If you want to/etc/passwd, start with the 2nd character of the sixth field to the 4th character, and then reverse the sort based on the first field.

cat/etc/passwd |  Sort-t ': '-K 6.2,6.4-k 1r      
bin:x : 2:2:bin:/bin:/bin/sh

See how many shells/etc/passwd have: sort the seventh field of the/etc/passwd, and then go to the weight:

cat/etc/passwd |  Sort-t ': '-K 7-u
daemon:x : 1:1:daemon:/usr/sbin:/bin/sh
sshd:x:104:65534::/var/run/sshd:/usr /sbin/nologin


The Uniq command can remove duplicate rows from a sorted file, so Uniq is often combined with sort. In other words, to make uniq work, all duplicate rows must be contiguous.

Uniq syntax

[Root@www ~]# Uniq [-icu]
options and Parameters:-I   : ignores uppercase and lowercase characters;-C: Counting-----  only displays unique rows

The contents of Testfile are as follows

Cat testfile

Delete the unsorted file directly, and you will find that no rows are deleted

#uniq testfile  

Sort files By default is to go to heavy

#cat Words | Sort |uniq

Duplicate rows are deleted after sorting, and the number of times the row is repeated at the beginning of the line

#sort Testfile | Uniq-c
1 friend
3 Hello
2 World

Show only duplicate rows and the number of times the row repeats at the beginning of the line

#sort Testfile | UNIQ-DC
3 Hello
2 World

Show only rows that are not duplicates

Sort Testfile | Uniq-u


The Cut command extracts text columns from a text file or text stream.

Cut syntax

[root@www ~]# cut-d ' delimited character '-f fields <== used with specific separator characters
[root@www ~]# cut-c character interval            <== for neat information
options and Parameters:-
D  : followed by a separator character. Used in conjunction with-
F  : Splits a piece of information into several paragraphs with-D, using-F to remove the meaning of paragraph,-
C  : The fixed character interval is taken out of the unit of character (characters);

The PATH variable is as follows

[Root@www ~]# echo $PATH
# 1 | 2       | 3   | 4       | 5            | 6            | 7

Take the path variable out and I'll find the fifth path.

#echo $PATH | Cut-d ': '-F 5

Take the path variable out and I'll find the third and fifth paths.

#echo $PATH | Cut-d ': '-F 3,5

Take the path variable out and I'm going to find the third to the last path.

echo $PATH | Cut-d ': '-f 3-

Take the path variable out and I'm going to find the first to the third path.

#echo $PATH | Cut-d ': '-f 1-3

Take the path variable out, I'm going to find the first to third, and the fifth path.

echo $PATH | Cut-d ': '-f 1-3,5

Practical example: Show only the/etc/passwd user and Shell

#cat/etc/passwd | Cut-d ': ' F 1,7 


Statistics the number of words in the file, how many lines, how many characters.

WC Grammar

[Root@www ~]# WC [-LWM]
options and Parameters:-
L  : Row only;-
W  : How many words are listed only (English word);
m  : how many characters;

Default use of WC statistics/etc/passwd

#wc/etc/passwd   1719/etc/passwd

40 is the number of lines, 45 is the number of words, 1719 is the number of bytes

The WC commands are relatively simple to use, with each parameter used as follows:

#wc-L/etc/passwd   #统计行数, the
40/etc/passwd       #表示系统有40个账户

#wc-W/etc/passwd are commonly used for the number of records  #统计单词出现次数

#wc-M  /etc/passwd
#统计文件的字节数 1719

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: 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.