sort

sort Command to File Sort the lines in the file specified by the parameter , And write the results to standard output . If File Parameter specifies multiple files , that sort Command to connect these files , And sort it as a file .

sort grammar

 

[[email protected] ~]# sort [-fbMnrtuk] [file or stdin]
Options and parameters :
-f  : Ignore case differences , for example  A  And  a  Same as code ;
-b  : Ignore the leading space part ;
-M  : Sort by month name , for example  JAN, DEC  And so on ;
-n  : Use 『 Pure number 』 Sort ( The default is to sort by text type );
-r  : Reverse sorting ;
-u  : Namely  uniq , In the same data , Only one line represents ;
-t  : Separator , The default is to use  [tab]  Key to separate ;
-k  : In that range  (field)  To sort 

 

 Yes /etc/passwd  To sort the account numbers of 

[[email protected] ~]# 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 By default, sort by first data , And the default is to sort by string , So the letters a Start ascending sort .

 

/etc/passwd The content is : To separate , I want to sort by the third column , How to

[[email protected] ~]# 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 use numeric sort :

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 , as follows

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

 

If you want to /etc/passwd, Let's start with the sixth field 2 Character to character 4 Forward sort of characters , And then reverse sort based on the first field .

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

 

see /etc/passwd How many shell: Yes /etc/passwd The seventh field of the , Then go heavy. :

cat /etc/passwd |  sort -t':' -k 7 -u
root:x:0:0:root:/root:/bin/bash
syslog:x:101:102::/home/syslog:/bin/false
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin

 

uniq

 uniq Command to remove duplicate lines from sorted files , therefore uniq Often with sort share . in other words , In order to make uniq Work , All repeating lines must be adjacent .

uniq grammar

[[email protected] ~]# uniq [-icu]
Options and parameters :
-i   : Ignore differences in case characters ;
-c  : Count
-u  : Show only one line 

 

testfile Is as follows

 

cat testfile
hello
world
friend
hello
world
hello

 

 

Delete unordered files directly , It will be found that no lines have been deleted

 

#uniq testfile  
hello
world
friend
hello
world
hello

 

 

Sort files , The default is to remove duplicate

#cat words | sort |uniq
friend
hello
world

 

After sorting, the duplicate lines are deleted , At the same time, output the number of times the line is repeated at the beginning of the line

#sort testfile | uniq -c
1 friend
3 hello
2 world

 

Show only duplicate lines , And show the number of times the line is repeated at the beginning of the line

#sort testfile | uniq -dc
3 hello
2 world

 

Show only lines that don't repeat

sort testfile | uniq -u
friend

 

cut

cut The command can extract text columns from a text file or text stream .

cut grammar

[[email protected] ~]# cut -d' Separator character ' -f fields <== Used with specific separator characters
[[email protected] ~]# cut -c  Character range             <== Used to arrange information in order
Options and parameters :
-d  : Followed by separator . And  -f  Use it together ;
-f  : basis  -d  The separator character divides a piece of information into segments , use  -f  Take out the meaning of the paragraph ;
-c  : In characters  (characters)  Fixed character range ;

 

PATH The variables are as follows

[[email protected] ~]# echo $PATH
/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games
# 1 | 2       | 3   | 4       | 5            | 6            | 7

 

take PATH Variable extraction , I want to find the fifth way .

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

 

take PATH Variable extraction , I'm going to find the third and fifth path .

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

 

take PATH Variable extraction , I want to find the third to the last path .

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

/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games

 

take PATH Variable extraction , I want to find the first to the third path .

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

/bin:/usr/bin:/sbin:

 

 

take PATH Variable extraction , 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

 

A practical example : Display only /etc/passwd Users and shell

#cat /etc/passwd | cut -d ':' -f 1,7 
root:/bin/bash
daemon:/bin/sh
bin:/bin/sh

 

 wc

How many words are there in the statistics file , How many rows? , How many characters? .

wc grammar

[[email protected] ~]# wc [-lwm]
Options and parameters :
-l  : Travel only ;
-w  : How many words are listed only ( English word );
-m  : How many characters? ;

 

By default wc Statistics /etc/passwd

#wc /etc/passwd
40   45 1719 /etc/passwd

40 It's the number of lines ,45 It's the number of words ,1719 Is the number of bytes

 

wc The command is relatively simple to use , Each parameter uses the following :

 

#wc -l /etc/passwd   # The statistical number of rows , When counting records , Very often
40 /etc/passwd       # Indicates that the system has 40 Accounts
#wc -w /etc/passwd  # Count the number of times words appear
45 /etc/passwd
#wc -m /etc/passwd  # Count the number of bytes in the file
1719

 

 

Reference resources  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