Linux/Unix排序、合并与分割

排序:sort
[oracle@localhost sort.folder]$ sort -c ls.out 
sort: ls.out:2: disorder: -rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out

表示未分类,使用 sort ls.out > ls.sort 将之分类重试:
[oracle@localhost sort.folder]$ sort ls.out > ls.sort
[oracle@localhost sort.folder]$ sort -c ls.sort

-k5表示根据第5行按降序排列:
[oracle@localhost sort.folder]$ sort -k 5 ls.out 
total 80
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 ls.out
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 sed.sh
-rwxr-xr-x 1 oracle oinstall   26 Nov 10 22:30 main.sh
-rwxrwxrwx 1 oracle oinstall   44 Jan  7 22:39 shelltest.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:51 test1.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:52 test2.sh
-rw-r--r-- 1 oracle oinstall  216 Feb 23 11:53 blank_file
-rwxr--r-- 1 oracle oinstall  642 Feb 15 18:43 find.sh
-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh
-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out
drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder

-k5表示根据第5行按降升序排列:
[oracle@localhost sort.folder]$ sort -k5 -r ls.out 
drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder
-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out
-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh
-rwxr--r-- 1 oracle oinstall  642 Feb 15 18:43 find.sh
-rw-r--r-- 1 oracle oinstall  216 Feb 23 11:53 blank_file
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:52 test2.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:51 test1.sh
-rwxrwxrwx 1 oracle oinstall   44 Jan  7 22:39 shelltest.sh
-rwxr-xr-x 1 oracle oinstall   26 Nov 10 22:30 main.sh
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 sed.sh
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 ls.out
total 80

返回排序后的第一行:
[oracle@localhost sort.folder]$ sort -k5 -r ls.out | head -1
drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder

返回排序后的最后两行:
[oracle@localhost sort.folder]$ sort -k5 -r ls.out | tail -2
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 ls.out
total 80

awk使用sort的输出结果:
[oracle@localhost sort.folder]$ sort -k5 -r ls.out | head -2 | awk '{print "file_name:"$9 " size:"$5}' 
file_name:folder size:4096
file_name:awk.out size:2653

某个文件中的内容如下:
[oracle@localhost sort.folder]$ cat iplist 
193.132.80.123 dave tansley
193.132.80.23  HP printer 2nd floor
193.132.80.198 JJ. Peter's scanner
193.132.80.38  SPARE
193.132.80.78  P.Edron
需要根据ip地址的最后一段进行排序:
[oracle@localhost sort.folder]$ sort -t. -k4 -r -n iplist
193.132.80.198 JJ. Peter's scanner
193.132.80.123 dave tansley
193.132.80.78  P.Edron
193.132.80.38  SPARE
193.132.80.23  HP printer 2nd floor
这里-r降序,-t.表示根据.进行分类,-n表示排序的是数值类型

uniq去重复用法:
其选项含义:
-u   只显示不重复行。
-d   只显示有重复数据行,每种重复行只显示其中一行
-c   打印每一重复行出现次数。
-f   n为数字,前n个域被忽略。

对已经排序的文件内容进行去重操作:
[oracle@localhost sort.folder]$ cat ls.sort 
total 80
total 80
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 ls.out
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 ls.out
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 sed.sh
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 sed.sh
-rwxr-xr-x 1 oracle oinstall   26 Nov 10 22:30 main.sh
-rwxr-xr-x 1 oracle oinstall   26 Nov 10 22:30 main.sh
-rwxrwxrwx 1 oracle oinstall   44 Jan  7 22:39 shelltest.sh
-rwxrwxrwx 1 oracle oinstall   44 Jan  7 22:39 shelltest.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:51 test1.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:51 test1.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:52 test2.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:52 test2.sh
-rw-r--r-- 1 oracle oinstall  216 Feb 23 11:53 blank_file
-rw-r--r-- 1 oracle oinstall  216 Feb 23 11:53 blank_file
-rwxr--r-- 1 oracle oinstall  642 Feb 15 18:43 find.sh
-rwxr--r-- 1 oracle oinstall  642 Feb 15 18:43 find.sh
-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh
-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh
-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out
-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out
drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder
drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder

[oracle@localhost sort.folder]$ uniq ls.sort 
total 80
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 ls.out
-rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 sed.sh
-rwxr-xr-x 1 oracle oinstall   26 Nov 10 22:30 main.sh
-rwxrwxrwx 1 oracle oinstall   44 Jan  7 22:39 shelltest.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:51 test1.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:52 test2.sh
-rw-r--r-- 1 oracle oinstall  216 Feb 23 11:53 blank_file
-rwxr--r-- 1 oracle oinstall  642 Feb 15 18:43 find.sh
-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh
-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out
drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder

显示重复的次数:
[oracle@localhost sort.folder]$ uniq -dc ls.sort 
      2 total 80
      2 -rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 ls.out
      2 -rw-r--r-- 1 oracle oinstall    0 Feb 24 14:21 sed.sh
      2 -rwxr-xr-x 1 oracle oinstall   26 Nov 10 22:30 main.sh
      2 -rwxrwxrwx 1 oracle oinstall   44 Jan  7 22:39 shelltest.sh
      2 -rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:51 test1.sh
      2 -rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:52 test2.sh
      2 -rw-r--r-- 1 oracle oinstall  216 Feb 23 11:53 blank_file
      2 -rwxr--r-- 1 oracle oinstall  642 Feb 15 18:43 find.sh
      2 -rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh
      2 -rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out
      2 drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder

下面ls.sort2文件中有如下内容:
[oracle@localhost sort.folder]$ cat ls.sort2      
0 oracle oinstall
0 oracle oinstall
0 oracle oinstall
0 oracle oinstall
26 oracle oinstall
26 oracle oinstall
44 oracle oinstall
44 oracle oinstall
99 oracle oinstall
99 oracle oinstall
99 oracle oinstall
99 oracle oinstall
216 oracle oinstall
216 oracle oinstall
642 oracle oinstall
642 oracle oinstall
2250 oracle oinstall
2250 oracle oinstall
2653 oracle oinstall
2653 oracle oinstall
4096 oracle oinstall
4096 oracle oinstall

去重复显示:
[oracle@localhost sort.folder]$ uniq -c ls.sort2  
      4 0 oracle oinstall
      2 26 oracle oinstall
      2 44 oracle oinstall
      4 99 oracle oinstall
      2 216 oracle oinstall
      2 642 oracle oinstall
      2 2250 oracle oinstall
      2 2653 oracle oinstall
      2 4096 oracle oinstall

使用-f前2个域忽略,则系统判断均为重复数据:
[oracle@localhost sort.folder]$ uniq -cf2 ls.sort2    
     22 0 oracle oinstall

join的用法:
有两个文件,内容如下:
[oracle@localhost sort.folder]$ cat names.txt 
M.Golls 12 Hidd Rd
P.Heller The Acre
P.Willey 132 The Grove
T.Norms 84 Connaught Rd
K.Fletch 12 Woodlea
[oracle@localhost sort.folder]$ cat town.txt 
M.Golls Norwith NRD
P.Willey Galashiels GDD
T.Norms Brandon BSL
K.Fletch Mildenhall MAF

我们来连接两个文件(其实类似于sql中的join):
[oracle@localhost sort.folder]$ join names.txt town.txt 
M.Golls 12 Hidd Rd Norwith NRD
P.Willey 132 The Grove Galashiels GDD
T.Norms 84 Connaught Rd Brandon BSL
K.Fletch 12 Woodlea Mildenhall MAF

显示第一个文件中未与第二个文件内容匹配的行:
[oracle@localhost sort.folder]$ join -a1 names.txt town.txt 
M.Golls 12 Hidd Rd Norwith NRD
P.Heller The Acre
P.Willey 132 The Grove Galashiels GDD
T.Norms 84 Connaught Rd Brandon BSL
K.Fletch 12 Woodlea Mildenhall MAF

显示第一个文件的第一个域和第二个文件的第二个域:
[oracle@localhost sort.folder]$ join -o 1.1,2.2 names.txt town.txt 
M.Golls Norwith
P.Willey Galashiels
T.Norms Brandon
K.Fletch Mildenhall

将文件1的域1和文件2的域1作为连接键:
[oracle@localhost sort.folder]$ join -j1 1 -j2 1 names.txt town.txt 
M.Golls 12 Hidd Rd Norwith NRD
P.Willey 132 The Grove Galashiels GDD
T.Norms 84 Connaught Rd Brandon BSL
K.Fletch 12 Woodlea Mildenhall MAF

cut用来剪切从标准输入或者文本中的列或者域。剪切的文本可以粘贴到文本文件中:
剪切以点号分割的第三个域:
[oracle@localhost sort.folder]$ cut -d. -f3 iplist 
80
80
80
80
80

paste粘贴:
[oracle@localhost sort.folder]$ ls | paste -d"" -
iplist
ls.out
ls.sort
ls.sort2
names.txt
paste1
paste2
sort.sh
town.txt

将两个文件分为两列粘贴,以冒号隔开:
[oracle@localhost sort.folder]$ paste -d: names.txt town.txt 
M.Golls 12 Hidd Rd:M.Golls Norwith NRD
P.Heller The Acre:P.Willey Galashiels GDD
P.Willey 132 The Grove:T.Norms Brandon BSL
T.Norms 84 Connaught Rd:K.Fletch Mildenhall MAF
K.Fletch 12 Woodlea:

split分割文件:
文件较大时可以使用此方法:
[oracle@localhost sort.folder]$ ll
total 64
-rw-r--r-- 1 oracle oinstall  143 Mar  3 10:58 iplist
-rw-r--r-- 1 oracle oinstall  620 Mar  3 10:51 ls.out
-rw-r--r-- 1 oracle oinstall 1240 Mar  3 11:14 ls.sort
-rw-r--r-- 1 oracle oinstall  436 Mar  3 11:25 ls.sort2
-rw-r--r-- 1 oracle oinstall  104 Mar  3 11:33 names.txt
-rw-r--r-- 1 oracle oinstall   12 Mar  3 12:07 paste1
-rw-r--r-- 1 oracle oinstall    0 Mar  3 12:04 paste2
-rwxr--r-- 1 oracle oinstall    0 Mar  3 09:51 sort.sh
-rw-r--r-- 1 oracle oinstall   88 Mar  3 11:35 town.txt

将文件分割为两行一个文件:
[oracle@localhost sort.folder]$ split -2 ls.out 
[oracle@localhost sort.folder]$ ll
total 112
-rw-r--r-- 1 oracle oinstall  143 Mar  3 10:58 iplist
-rw-r--r-- 1 oracle oinstall  620 Mar  3 10:51 ls.out
-rw-r--r-- 1 oracle oinstall 1240 Mar  3 11:14 ls.sort
-rw-r--r-- 1 oracle oinstall  436 Mar  3 11:25 ls.sort2
-rw-r--r-- 1 oracle oinstall  104 Mar  3 11:33 names.txt
-rw-r--r-- 1 oracle oinstall   12 Mar  3 12:07 paste1
-rw-r--r-- 1 oracle oinstall    0 Mar  3 12:04 paste2
-rwxr--r-- 1 oracle oinstall    0 Mar  3 09:51 sort.sh
-rw-r--r-- 1 oracle oinstall   88 Mar  3 11:35 town.txt
-rw-r--r-- 1 oracle oinstall  108 Mar  3 12:14 xaa
-rw-r--r-- 1 oracle oinstall  113 Mar  3 12:14 xab
-rw-r--r-- 1 oracle oinstall  109 Mar  3 12:14 xac
-rw-r--r-- 1 oracle oinstall  111 Mar  3 12:14 xad
-rw-r--r-- 1 oracle oinstall  116 Mar  3 12:14 xae
-rw-r--r-- 1 oracle oinstall   63 Mar  3 12:14 xaf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值