[Linux study notes] sort | uniq | cut | wc (my first markdown note)

Source: Internet
Author: User
The sortsort 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

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] [file or stdin]
Options and parameters:
-F: ignore case-insensitive differences. 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, -n: sort by "pure number" (by default, it is sorted by text type);-r: reverse sorting;-u: uniq, in the same data, only one row is represented.-t: delimiter, which is separated by the [tab] Key by default.-k: indicates the range (field) for sorting.

Sort/etc/passwd accounts

[root@www ~]# cat /etc/passwd | sort 

Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
Apache: x: 48: 48: Apache:/var/www:/sbin/nologin
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: 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:/bin/bash
Uucp: x: 10: 14: uucp:/var/spool/uucp:/sbin/nologin
Operator: x: 11: 0: operator:/root:/sbin/nologin
Bin: x: 1: 1: bin:/sbin/nologin
Games: 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 ':' -k 3n

Root: x: 0: 0: root:/bin/bash
Daemon: x: 1: 1: daemon:/usr/sbin:/bin/sh
Bin: 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 3nr

Nobody: x: 65534: 65534: nobody:/nonexistent:/bin/sh
Ntp: x: 106: 113:/home/ntp:/bin/false
Messagebus: x: 105: 109:/var/run:/bin/false
Sshd: x: 104: 65534:/var/run/sshd:/usr/sbin/nologin

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

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: ignore case-insensitive characters;-c: Count-u: only show unique rows

The content of testfile is as follows:

cat testfile  

Hello
World
Friend
Hello
World
Hello

Directly delete unordered files. no rows are deleted.

uniq testfile  

Hello
World
Friend
Hello
World
Hello

Sort files. deduplication is used by default.

cat words | sort |uniq

Friend
Hello
World

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

sort testfile | uniq -c

1 friend
3 hello
2 world

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

sort testfile | uniq -dc

3 hello
2 world

Only show non-repeated rows

sort testfile | uniq -u

Friend

Cut

The cut command can 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 <= used to arrange neatly
Options and parameters:
-D: followed by separator characters. Used with-f;-f: splits a piece of information into segments based on the-d separator, and uses-f to get the meaning of the segments;-c: take out a fixed character range in units of characters (characters;

The PATH variable is as follows:

[root@www ~]# echo $PATH/bin:/usr/bin:/sbin:/usr/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 ':' -f 5/usr/local/bin

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

echo $PATH | cut -d ':' -f 3,5/sbin:/usr/local/bin

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

echo $PATH | cut -d ':' -f 3-/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games

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

echo $PATH | cut -d ':' -f 1-3,5/bin:/usr/bin:/sbin:/usr/local/bin
Wc

The number of words, lines, and characters in the statistical file.

Wc syntax
[root@www ~]# wc [-lwm]
Options and parameters:
-L: Only list rows;-w: only how many words are listed (single words);-m: How many characters are listed;

Wc statistics are used by default./etc/passwd

wc /etc/passwd40   45 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 statistical rows, usually 40/etc/passwd # indicates that the system has 40 accounts wc-w/etc/passwd # count the number of word occurrences 45/etc/passwdwc-m/etc/passwd # statistical file 1719 bytes

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.