sort/uniq/cut/wc/

Source: Internet
Author: User
Tags sorts syslog
Sort

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

Sort syntax

[Root@www ~]# Sort [-fbmnrtuk] [file or stdin]
options and Parameters:-
F  : Ignores case differences, such as a and a as encoding;
-B  : Ignores the front spaces part; -M: Sort by the name of the  month, such as the sorting method of a few lines, DEC, and so on;-
n  : Sort using "pure numbers" (default is sorted by text type);
r  : reverse sort;  : Is Uniq, in the same data, only one row is represented;-
T  : separator, by default, by using the [tab] key to separate;
K  : The meaning of sorting by that interval (field)
Sort the accounts 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 by default it is sorted as a string, so the beginning of the letter A is sorted in ascending order.

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

[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 use numeric sorting:

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, start with the 2nd character of the sixth field to the 4th character, and then reverse the 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 how many shells/etc/passwd have: sort the seventh field of the/etc/passwd, and then go to the weight:

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

The Uniq command can remove duplicate rows from a sorted file, so Uniq is often combined with sort. In other words, to make uniq work, all duplicate rows must be contiguous.

Uniq syntax

[Root@www ~]# Uniq [-icu]
options and Parameters:-I   : ignores uppercase and lowercase characters;-C: Counting-----  only displays unique rows

The contents of Testfile are as follows

Cat testfile
Hello
world
friend
Hello
world
Hello

Delete the unsorted file directly, and you will find that no rows are deleted

#uniq testfile  
Hello
world
friend
Hello
world
Hello

Sort files By default is to go to heavy

#cat Words | Sort |uniq
friend
Hello
World

Duplicate rows are deleted after sorting, and 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 duplicate rows and the number of times the row repeats at the beginning of the line

#sort Testfile | UNIQ-DC
3 Hello
2 World

Show only rows that are not duplicates

Sort Testfile | Uniq-u
Friend  

Cut-and

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

Cut syntax

[root@www ~]# cut-d ' delimited character '-f fields <== used with specific separator characters
[root@www ~]# cut-c character interval            <== for neat information
options and Parameters:-
D  : followed by a separator character. Used in conjunction with-
F  : Splits a piece of information into several paragraphs with-D, using-F to remove the meaning of paragraph,-
C  : The fixed character interval is taken out of the unit of character (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 out and I'll find the fifth path.

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

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

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

Take the path variable out and I'm going 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 the path variable out and I'm going to find the first to the third path.

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

Take the path variable out, I'm going to find the first to third, and the fifth path.

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

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

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

WC

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

WC Grammar

[Root@www ~]# WC [-LWM]
options and Parameters:-
L  : Row only;-
W  : How many words are listed only (English word);
m  : how many characters;

Default use of WC statistics/etc/passwd

#wc/etc/passwd   1719/etc/passwd

40 is the number of lines, 45 is the number of words, 1719 is the number of bytes

The WC commands are relatively simple to use, with each parameter used as follows:

#wc-L/etc/passwd   #统计行数, the
40/etc/passwd       #表示系统有40个账户

#wc-W/etc/passwd are commonly used for the number of records  #统计单词出现次数
45/etc/passwd

#wc-M  /etc/passwd
#统计文件的字节数 1719

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.