Linux SORT,UNIQ,CUT,WC.

Source: Internet
Author: User
Tags sorts

Article transferred from http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858385.html

Sort

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

Sort syntax

Sort [-fbmnrtuk] [file  or stdin] options and Parameters:-F  : Ignores case differences, for example A and a are treated as encodings;-B  : Ignores the first whitespace part;-M : Sort by  the name of the month, such as the sort method of JAN, DEC and so on;-n  : Sort using "pure number" (by default it is sorted by text type);-R  : reverse sort;-U  uniq  , in the same data, only one row is represented;-t  : delimiter, by default, is delimited by the [tab] key;-K  : With that range (field) To sort the meaning of
Sort the accounts of the/etc/passwd
cat /etc/passwdsortadm:x:3:4: adm:/var/adm:/sbin/  Nologinapache:x: $:apache:/var/www:/sbin/nologinbin:x:1:1 : bin:/bin:/sbin/nologindaemon:x:2:2:d aemon:/sbin:/sbin/nologin

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

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

[Email protected] ~]#Cat/etc/passwd|Sort-T':'-K3root:x:0:0: root:/root:/bin/bashuucp:x:Ten: -: uucp:/var/spool/uucp:/sbin/nologinoperator:x: One:0: operator:/root:/sbin/nologinbin:x:1:1: bin:/bin:/sbin/nologingames:x: A: -: Games:/usr/games:/sbin/nologin

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

cat /etc/passwdsort':' -k 3nroot:x:0:  0: root:/root:/bin/bashdaemon:x:1:1:d aemon:/usr/sbin:/bin/sh 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 3nrnobody:x:65534:65534: nobody:/nonexistent:/bin/SHntp:x:106:113::/home/ntp:/bin/falsemessagebus:x: the:109::/var/run/dbus:/bin/falsesshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin

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

Cat/etc/passwd|Sort-T':'-K6.2,6.4-k 1rSync: x:4:65534:Sync:/bin:/bin/Syncproxy:x: -: -:p roxy:/bin:/bin/SHbin:x:2:2: bin:/bin:/bin/SHsys:x:3:3: sys:/dev:/bin/SH

See how many shells the/etc/passwd has: sort the seventh field of/etc/passwd and then go back to the heavy one:

Cat/etc/passwd|Sort-T':'-K7-uroot:x:0:0: root:/root:/bin/bashsyslog:x:101:102::/home/syslog:/bin/falsedaemon:x:1:1:d aemon:/usr/sbin:/bin/SHSync: x:4:65534:Sync:/bin:/bin/Syncsshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin

Uniq

The Uniq command removes duplicate rows from a sorted file, so Uniq is often used in combination with sort. That is, in order for the Uniq to work, all duplicate rows must be contiguous.

Uniq syntax

Uniq [-ICU] options and Parameters:-i   : ignore the difference between uppercase and lowercase characters;-C  : Count -u  : Only show unique rows

The contents of Testfile are as follows

Cat Testfilehelloworldfriendhelloworldhello

Deleting an unordered file directly will reveal that no rows have been deleted

#Uniq  testfile  Helloworldfriendhelloworldhello

Sort files, the default is to go to heavy

#CatSort | Uniq Friendhelloworld

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

#Sortuniq -c1  friend3  Hello2 World

Displays only duplicate rows, and displays the number of repetitions of the row at the beginning of the line

#Sortuniq -DC3  Hello2 World

Show only rows that are not duplicates

Sort uniq -ufriend  
Cut

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

Cut grammar

cut -D' delimited characters ' -Ffields <==cut -C character interval            <==  Used for neatly arranged information options and parameters:  -D: followed by delimited characters. Used inconjunctionwith-F  : Divides a piece of information into paragraphs according to the delimiter character of-D, and uses-F to remove the meaning of theparagraph;-C  : Remove the fixed character interval in the unit of character (characters);

The PATH variable is as follows

Echo $PATH/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/x11r6/bin:/usr/12        3   4       5            6            7

To take the path variable out, I'm going to find a fifth path.

#Echocut':'5/usr/local/bin

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

#Echocut':'3,5/sbin:/usr/local/bin

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

Echo Cut ' : ' 3-/sbin:/usr/sbin:/usr/local/bin:/usr/x11r6/bin:/usr/games

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

#Echocut':'1-3/bin:/usr/bin:/sbin:

Take the path variable out, I want to find the first to third, there is a fifth path.

Echo Cut ' : ' 1-3,5/bin:/usr/bin:/sbin:/usr/local/bin

Practical Example: Display only/etc/passwd users and shells

#Cat /etc/passwdcut':'1,7  root: /bin/Bashdaemon:/bin/shbin:/bin/sh

WC

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

WC syntax

WC [-LWM] options and Parameters:-L  : List only travel;-w  : How many words are listed (English word);-M  : how many characters;

Use WC statistic/etc/passwd by default

#WC /etc/passwd   1719 /etc/passwd

40 is the number of rows, 45 is the number of words, and 1719 is the number of bytes

The WC commands are relatively simple to use and each parameter is used as follows:

#WC -l/etc/passwd   #统计行数, very common/etc/passwd #表示系统有40个账户 when it comes to recording numbers         WC -w /etc/passwd  #统计单词出现次数/etc/passwd#WC -m/etc/passwd  #统计文件的字节数

Linux SORT,UNIQ,CUT,WC.

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.