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
[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地址的最后一段进行排序: