Linux command-SED,UNIQ,CUT,WC

Source: Internet
Author: User
Tags sorts

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

[[email protected] ~]# sort [-fbmnrtuk] [file or stdin] options and Parameters:-F  : Ignores case differences, such as 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, sort by text type);-R  : reverse sort;-u  : Uniq, the same data, only one row is represented;-T  : delimiter, which is delimited by the [tab] key, and-K  : The meaning of sorting by that interval (field)

Sort the accounts of the/etc/passwd
[Email protected] ~]# CAT/ETC/PASSWD | sortadm:x:3:4:adm:/var/adm:/sbin/nologinapache:x:48:48:apache:/var/www:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/ Nologindaemon:x:2:2:daemon:/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 ': '-K 3root:x:0:0:root:/root:/bin/bashuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:o Perator:/root:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin

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

cat/etc/passwd | Sort-t ': '-K 3nroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin: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 : 105: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 ': '-K 6.2,6.4-k 1r      sync:x:4:65534:sync:/bin:/bin/syncproxy:x:13:13:proxy:/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 ': '-K 7-uroot:x:0:0:root:/root:/bin/bashsyslog:x:101:102::/home/syslog:/bin/falsedaemon:x:1:1:daemon:/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

[[email protected] ~]# uniq [-ICU] options and Parameters:   -I: Ignoring 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

#cat Words | Sort |uniqfriendhelloworld

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

#sort Testfile | UNIQ-C1 Friend3 Hello2 World

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

#sort Testfile | UNIQ-DC3 Hello2 World

Show only rows that are not duplicates

Sort Testfile | Uniq-ufriend  

Cut

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

Cut grammar

[[email protected] ~]# cut-d ' The delimiter character '-f fields <== used to have a specific delimiter character [[email protected] ~]# cut-c character interval            <== used to arrange neat information options and parameters:- D  : followed by delimited characters. Use with-F;  -F: Divides a piece of information into several segments according to the delimiter character of-D, and uses-F to remove the meaning of the paragraph;-C  : Remove the fixed character interval in the unit of character (characters);

The PATH variable is 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

To take the path variable out, I'm going to find a 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'll 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 out and I'll find the first to third path.

#echo $PATH | Cut-d ': '-f 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 $PATH | Cut-d ': '-f 1-3,5
/bin:/usr/bin:/sbin:/usr/local/bin

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

#cat/etc/passwd | Cut-d ': '-f 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

[[email protected] ~]# WC [-LWM] options and Parameters:  -L: Row only;  -W: Lists only how many words (English word);-M  : how many characters;

Use WC statistic/etc/passwd by default

#wc/ETC/PASSWD40   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   #统计行数, 40/etc/passwd       #表示系统有40个账户 #wc-w/etc/passwd  #统计单词出现次数45/etc/passwd# When it comes to the number of records Wc-m/etc/passwd  #统计文件的字节数1719

Reference 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

Linux command-SED,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.