Linux sort, uniq, cut, WC command details

Source: Internet
Author: User
Tags month name sorts
Sort

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

Sort syntax

 
[Root @ WWW ~] #Sort[-Fbmnrtuk] [FileOr stdin] Options and parameters:-F: Case sensitivity differences are ignored. For example, a and a are considered to be the same encoding;-B: Ignore the leading space character;-M: sort by month name, such as Jan and Dec;-N: Use "numbers only" for sorting (by default, it is sorted by text type );-R: reverse sorting;-U: YesUniqIn the same data, only one row is displayed;-T: delimiter, which is separated by the [Tab] key by default;-K: the meaning of sorting by field

Sort/etc/passwd accounts
 
[Root @ WWW ~] #Cat/Etc/Passwd|SortADM: X:3:4: ADM:/var/adm:/sbin/Nologinapache: X:48:48: Apache:/var/www:/sbin/Nologinbin: X:1:1: Bin:/bin:/sbin/Nologindaemon: X:2:2: Daemon:/sbin/nologin

By default, sort is sorted by the first data, and is sorted by string by default.

 

The/etc/passwd content is separated by:. I want to sort it in the third column.

[Root @ WWW ~] # Cat /Etc/ Passwd | Sort -T '  :  ' -K 3  Root: X:  0 : 0 : Root:/root:/bin/ Bashuucp: X:  10 :14 : Uucp:/var/spool/uucp:/sbin/ Nologinoperator: X:  11 : 0 : Operator:/root:/sbin/ Nologinbin: X:  1 : 1 : Bin:/bin:/sbin/ Nologingames: X:  12 : 100 : Games:/usr/games:/sbin/nologin

It is sorted by string by default. If you want to sort by number:

Cat/Etc/Passwd|Sort-T':'-K3nRoot: X:0:0: Root:/root:/bin/Bashdaemon: X:1:1: Daemon:/usr/sbin:/bin/ShBin: X:2:2: Bin:/bin/Sh

The default value is ascending. If you want to sort in descending order

 Cat /Etc/ Passwd |Sort -T '  :  ' - K 3 nrnobody: X:  65534 : 65534 : Nobody:/nonexistent:/bin/ Sh  NTP: X:  106 : 113 :/Home/NTP:/bin/ False  Messagebus: X:  105 : 109 :/Var/run/restart:/bin/ False  Sshd: X:  104 : 65534 :/Var/run/sshd:/usr/sbin/nologin

 

If you want to forward/etc/passwd, sort the data by forward from 2nd to 4th characters in the sixth domain, and then sort the data by reverse direction based on the first domain.

 Cat /Etc/ Passwd | Sort -T '  :  ' -K 6.2 , 6.4 -K 1R  Sync : X: 4 : 65534 : Sync :/Bin/ Sync  Proxy: X:  13 : 13 : Proxy:/bin/ Sh  Bin: X:  2 : 2 : Bin:/bin/ Sh  SYS: X: 3 : 3 : SYS:/dev:/bin/ Sh 

 

Check the number of shells in/etc/passwd: sort the seventh domain of/etc/passwd, and then deduplicate:

 Cat /Etc/ Passwd | Sort -T '  :  ' -K 7 - Uroot: X:  0 : 0 : Root:/root:/bin/Bashsyslog: X:  101 : 102 :/Home/syslog:/bin/ False  Daemon: X:  1 : 1 : Daemon:/usr/sbin:/bin/ Sh  Sync : X: 4 : 65534 : Sync :/Bin/ Sync  Sshd: X: 104 : 65534 :/Var/run/sshd:/usr/sbin/nologin

 

Uniq

 The uniq command can remove duplicate rows from sorted files. Therefore, uniq is often used with sort. That is to say, to make uniq work, all repeated rows must be adjacent.

Uniq syntax

 
[Root @ WWW ~] #Uniq[-ICU] Options and parameters:-I: case-insensitive characters are ignored;-C: Count-U: only display unique rows

 

The content of testfile is as follows:

 
CatTestfilehelloworldfriendhelloworldhello

 

Directly Delete unordered files. No rows are deleted.

#UniqTestfile helloworldfriendhelloworldhello

 

Sort files. deduplication is used by default.

 
# CatWords |Sort|UniqFriendhelloworld

 

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

 
# SortTestfile |Uniq-C1Friend3Hello2World

 

Only duplicate rows are displayed, and the number of repeated rows is displayed at the beginning of the row.

#SortTestfile |Uniq-DC3Hello2World

 

Only show non-repeated rows

 
SortTestfile |Uniq-Ufriend

 

Cut

CutCommand to extract text columns from a text file or text stream.

Cut syntax

[Root @ WWW ~] #Cut-D'Delimiter'-F fields <=Used to have specific delimiter [root @ WWW~] #Cut-C character range <=Information options and parameters used for sorting:-D: followed by separator characters. And-F is used together;-F: segments are separated into segments based on the-D delimiter-F obtain the meaning of the nth segment;-C: extracts a fixed character range in units of characters (characters;

 

The PATH variable is as follows:

[Root @ WWW ~] #Echo$ Path/Bin:/usr/bin:/sbin:/usr/local/bin:/usr/x11r6/bin:/usr/Games #1|2|3|4|5|6|7

 

Remove the PATH variable. I want to find the fifth path.

 
#Echo$ PATH |Cut-D':'-F5/Usr/local/bin

 

Remove the PATH variable. I want to find the third and fifth paths.

#Echo$ PATH |Cut-D':'-F3,5/Sbin:/usr/local/bin

 

Remove the PATH variable. I want to find the third to last path.

 
Echo$ PATH |Cut-D':'-F3-
 
/Sbin:/usr/local/bin:/usr/x11r6/bin:/usr/Games

 

Remove the PATH variable. I want to find the first to third paths.

#Echo$ PATH |Cut-D':'-F1-3
 
/Bin:/usr/bin:/sbin:
 
 

 

Remove the PATH variable. I want to find the first, third, and fifth paths.

 
Echo$ PATH |Cut-D':'-F1-3,5
 
/Bin:/usr/bin:/sbin:/usr/local/bin

 

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

#Cat/Etc/Passwd|Cut-D':'-F1,7Root:/Bin/Bashdaemon:/Bin/ShBin:/Bin/Sh

 

WC

StatisticsThe number of words, lines, and characters in the file.

WC syntax

[Root @ WWW ~] #WC[-LWM] Options and parameters:-L: only list rows;-W: Only the number of words listed (one English word );-M: The number of characters;

 

WC statistics are used by default./etc/passwd

 
#WC/Etc/Passwd4045 1719/Etc/Passwd

40 indicates the number of rows, 45 indicates the number of words, and 1719 indicates the number of bytes.

 

WC commands are relatively simple to use. Each parameter is as follows:

#WC-L/etc/Passwd# Number of Statistics rows, which are commonly used in the number of records40/Etc/Passwd # indicates that the system has 40 accounts#WC-W/Etc/Passwd# Count the number of times a word appears45/Etc/Passwd#WC-M/etc/Passwd# Count the number of bytes of a file1719

 

Reference http://vbird.dic.ksu.edu.tw/linux_basic/0320bash_6.php#pipe_2

http://www.cnblogs.com/stephen-liu74/archive/2011/11/10/2240461.html

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.