Linux sort,uniq,cut,wc命令详解

Sort

sort命令对File参数指定的文件中的行排序,并将结果写到标准输出。如果File参数指定多个文件,那么sort命令将这些写文件连接起来,并当作一个文件进行排序。

sort语法

[root@www ~]# sort [-fbMnrtuk] [file or stdin]选项与参数:-f  :忽略大小写的差异,例如 A 与 a 视为编码相同;-b  :忽略最前面的空格符部分;-M  :以月份的名字来排序,例如 JAN, DEC 等等的排序方法;-n  :使用『纯数字』进行排序(默认是以文字型态来排序的);-r  :反向排序;-u  :就是 uniq ,相同的数据中,仅出现一行代表;-t  :分隔符,默认是用 [tab] 键来分隔;-k  :以那个区间 (field) 来进行排序的意思

对/etc/passwd的账号进行排序

[root@www ~]# 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是默认以第一个数据来排序,而且默认是以字符串形式来排序,所以由字母a开始升序排序。

/etc/passwd 内容是以:来分割的,我想以第三栏来排序,该如何

[root@www ~]# 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:operator:/root:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin

注:-t指定分隔符,-k指定按第几列排序。

默认是以字符串来排序的,如果想要使用数字排序:

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

注:-n表示以数字的形式进行排序。

默认是升序排序,如果要倒叙排序,如下:

[root@liaozhongmin5 lavimer]# cat passwd | sort -t ':' -k 3 -n -rnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinzhong-10:x:533:533::/home/zhong-10:/bin/bashzhong-09:x:532:532::/home/zhong-09:/bin/bashzhong-08:x:531:531::/home/zhong-08:/bin/bashzhong-07:x:530:530::/home/zhong-07:/bin/bashzhong-06:x:529:529::/home/zhong-06:/bin/bashzhong-05:x:528:528::/home/zhong-05:/bin/bashzhong-04:x:527:527::/home/zhong-04:/bin/bashzhong-03:x:526:526::/home/zhong-03:/bin/bashzhong-02:x:525:525::/home/zhong-02:/bin/bashzhong-01:x:524:524::/home/zhong-01:/bin/bash

如果要对/etc/passwd,先以第六个域的第二个字符到第4个字符进行正向排序,再基于第一个域进行反向排序。

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

注:可以使用两个-k参数。

查看/etc/passwd有多少个shell;对/etc/passwd的第七个域进行排序,然后去重:

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

uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用。也就是说,为了使uniq起作用,所有的重复行必须是相邻的。

uniq语法

[root@www ~]# uniq [-icu]选项与参数:-i   :忽略大小写字符的不同;-c  :进行计数-u  :只显示唯一的行

testfile的内容如下:

cat testfilehelloworldfriendhelloworldhello

注:直接uniq未经排序的文件,将会发现没有任何行被删除

对文件内容进行排序之后,在进行去重:

#cat words | sort |uniqfriendhelloworld

排序之后删除了重复行,同时在行首位置输出该行重复的次数

#sort testfile | uniq -c1 friend3 hello2 world

仅显示存在重复的行,并在行首显示该行重复的次数

#sort testfile | uniq -dc3 hello2 world

仅显示不重复的行

sort testfile | uniq -ufriend  

cut

cut命令可以从一个文本文件或者文本流中提取文本列

cut语法

[root@www ~]# cut -d'分隔字符' -f fields <==用于有特定分隔字符[root@www ~]# cut -c 字符区间            <==用于排列整齐的信息选项与参数:-d  :后面接分隔字符。与 -f 一起使用;-f  :依据 -d 的分隔字符将一段信息分割成为数段,用 -f 取出第几段的意思;-c  :以字符 (characters) 的单位取出固定字符区间;

PATH变量如下

[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

将PATH变量取出,我要找出第五个路径

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

将PATH变量取出,我要找出第三个和第五个路径

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

将PATH变量取出,我要找出第三到最后一个路径

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

将PATH变量取出,我要找出第一到第三个路径

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

将PATH变量取出,我要找出第一到第三,还有第五个路径

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

实用例子:只显示/etc/passwd的用户和shell

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

WC

统计文件里面有多少个单词,多少行,多少字符。

wc语法

[root@www ~]# wc [-lwm]选项与参数:-l  :仅列出行;-w  :仅列出多少字(英文单字);-m  :多少字符;

使用wc统计/etc/passwd的情况

#wc /etc/passwd40   45 1719 /etc/passwd

注:40表示行数,45表示单词数,1719表示字节数。

wc的命令比较简单,每个参数的使用如下:

#wc -l /etc/passwd   #统计行数,在对记录数时,很常用40 /etc/passwd       #表示系统有40个账户#wc -w /etc/passwd  #统计单词出现次数45 /etc/passwd#wc -m /etc/passwd  #统计文件的字节数1719

文章来自:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858385.html

即使爬到最高的山上,一次也只能脚踏实地地迈一步。

Linux sort,uniq,cut,wc命令详解

相关文章:

你感兴趣的文章:

标签云: