Sort|uniq| Cut| Wc (my first markdown note)

  

sort

The sort command sorts the lines in the file specified by the File parameter and writes the result to standard output. If the File parameter specifies more than one file, the sort command concatenates the files and sorts them as one file.

sort syntax

[root@www ~]# sort [-fbMnrtuk] [file or stdin]
Options and parameters:
-f : ignore case Differences, such as A and a are treated as the same encoding; -b: ignore the leading space character portion; -M: sort by month name, such as JAN, DEC, etc.; -n : use "pure numbers" Sorting (default is sorted by text type); -r: reverse sorting; -u: is uniq, only one line is represented in the same data; -t : separator, default is [tab] The key is used to separate; -k : the meaning of sorting by that field 

Sorting the account of /etc/passwd

[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:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin

sort is sorted by default with the first data, and the default is in string form To sort, so sort by ascending alphabet a.

The contents of /etc/passwd are separated by :, I want to sort by the third column:

[root@www ~]# cat /etc/passwd |
  Sort -t ':' -k 3

root:x:0:0:root:/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:/bin:/sbin/nologin games:x:12:100:games:/usr/games:/Sbin/nologin

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

cat /etc/passwd |
  Sort -t ':' -k 3n

root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/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, the following

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/dbus:/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:/bin/sync proxy:x:13:13:proxy:/bin:/bin/Sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh

uniq

uniq command You can remove duplicate lines from sorted files, so uniq is often used with sort. That is, in order for uniq to work, all duplicate rows must be contiguous.

uniq syntax

[root@www ~]# uniq [-icu]

Options and parameters:

-i : ignore the difference between upper and lower case characters; c : Counting -u : only shows the unique line 

The contents of the testfile are as follows

cat testfile 

hello world friend hello world hello

directly delete unsorted files, Will find that no rows are deleted

uniq testfile 

hello world friend hello world hello

Sort files, default is to re-

cat words |
  Sort |
 Uniq

friend hello world

Delete duplicate rows after sorting, and output 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 the number of duplicate rows and show the number of iterations at the beginning of the line

sort testfile |
  Uniq -dc

3 hello 2 world

Show only rows that are not duplicate

sort testfile |
  Uniq -u

friend

cut

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

cut syntax

[root@www ~]# cut -d' separator character ' -f fields <==for specific separator characters [root@www ~]# cut - c Character interval <== for neatly arranged information 

Options and parameters:

-d : followed by a separator character. Used with -f; -f : Splits a piece of information into segments according to the separator character of -d, and extracts the meaning of the first paragraph with -f; -c : takes a fixed character range in units of 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

Take the PATH variable and I will find the fifth path.

echo $PATH |
  Cut -d ':' -f 5/usr/local/bin

Take the PATH variable and I will find the third and fifth paths.

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

Take the PATH variable and I will find the third to last path.

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

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

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

wc

How many words and how many lines are in the statistics file? How many characters.

wc syntax

[root@www ~]# wc [-lwm]

Options and parameters:

-l : ​​list only rows; -w : Only how many words (English words) are listed; -m : How many characters; 

Use wc statistics by default /etc/passwd

wc /etc/passwd40 45 1719 /etc/passwd

40 is OK Number, 45 is the number of words, 1719 is the number of bytes

The command of wc is relatively simple to use, each parameter is used as follows:

wc -l /etc/passwd #Statistic line number, in For the number of records, it is very common 40 /etc/passwd # indicates that the system has 40 accounts wc -w /etc/passwd #Statistics word occurrences 45 /etc/passwdwc -m /etc/passwd #Bytes of statistics file 1719
Copyright © Windows knowledge All Rights Reserved