linux 文本排序、合并和分割 -转

来源:互联网 发布:园区网络设计方案 编辑:程序博客网 时间:2024/06/04 18:21

sort [选项] [输入文件](文本排序)
选项:
-c 测试文件是否已经被排序
-k 指定排序的域
-m 合并两个已排序的文件
-n 根据数字大小进行排序
-o[输出文件] 当输出写到指定的文件
-r 将排序结果逆向显示
-t 改变域分隔符
-u 去除结果中的重复行

示例:
1、以第二列排序
[sleep@localhost greptest]$ sort -t: -k2  frut.txt
pingguo:apple:12:lhwl:2012-11-01
xiangjiao:banana:14:lhwl:2012-09-09
juzi:orange:10:lhzxt:2012-10-11
shizi:shizi:8:herj:2012-12-08

2、以第三列价格排序
[sleep@localhost greptest]$ sort -t: -k3 -n  frut.txt  (sort -t: -k3n  frut.txt)
shizi:shizi:8:herj:2012-12-08
juzi:orange:10:lhzxt:2012-10-11
pingguo:apple:12:lhwl:2012-11-01
xiangjiao:banana:14:lhwl:2012-09-09

3、以第三列价格倒序排列
[sleep@localhost greptest]$ sort -t: -k3nr  frut.txt
xiangjiao:banana:14:lhwl:2012-09-09
pingguo:apple:12:lhwl:2012-11-01
juzi:orange:10:lhzxt:2012-10-11
shizi:shizi:8:herj:2012-12-08

uniq [选项] [文件] (去除文本重复的行,只有相邻的行,重复才去除,隔行不去除)
选项:
-c 打印每行在文本中重复出现的次数
-d 只显示有重复的记录,每个重复记录只出现一次
-u 只显示没有重复的记录

1、uniq 和 sort -u 的区别
[sleep@localhost greptest]$ cat frut.txt
pingguo:apple:12:lhwl:2012-11-01
juzi:orange:10:lhzxt:2012-10-11
juzi:orange:10:lhzxt:2012-10-11

boluo:boluo:12:2012-11-11
juzi:orange:10:lhzxt:2012-10-11
xiangjiao:banana:14:lhwl:2012-09-09
shizi:shizi:8:herj:2012-12-08
[sleep@localhost greptest]$ sort -t: -u frut.txt
boluo:boluo:12:2012-11-11
juzi:orange:10:lhzxt:2012-10-11
pingguo:apple:12:lhwl:2012-11-01
shizi:shizi:8:herj:2012-12-08
xiangjiao:banana:14:lhwl:2012-09-09
[sleep@localhost greptest]$ uniq frut.txt
pingguo:apple:12:lhwl:2012-11-01
juzi:orange:10:lhzxt:2012-10-11
boluo:boluo:12:2012-11-11
juzi:orange:10:lhzxt:2012-10-11
xiangjiao:banana:14:lhwl:2012-09-09
shizi:shizi:8:herj:2012-12-08


2、显示文本重复的次数
[sleep@localhost greptest]$ uniq -c frut.txt
      1 pingguo:apple:12:lhwl:2012-11-01
      2 juzi:orange:10:lhzxt:2012-10-11
      1 boluo:boluo:12:2012-11-11
      1 juzi:orange:10:lhzxt:2012-10-11
      1 xiangjiao:banana:14:lhwl:2012-09-09
      1 shizi:shizi:8:herj:2012-12-08

cut [选项] 文件 (提取文本)
选项:
-c 指定提取的字符数或字符范围 -cn 第n个字符 -cn,m 第n个和第m个字符 -cn-m 第n到m个字符
-f 指定提取的域数或范围
-d 改变域分隔符

示例:
1、提取第一个字符
[sleep@localhost greptest]$ cut -c1 frut.txt
p
j
j
b
j
x
s
2、提取第一个和第五个字符
[sleep@localhost greptest]$ cut -c1,5 frut.txt
pg
j:
j:
bo
j:
xg
si
3、提取第一个到第五个字符
[sleep@localhost greptest]$ cut -c1-5 frut.txt
pingg
juzi:
juzi:
boluo
juzi:
xiang
shizi
4、提取第三域和第五域
[sleep@localhost greptest]$ cut -d: -f3,5 frut.txt
12:2012-11-01
10:2012-10-11
10:2012-10-11
12:2012-11-11
10:2012-10-11
14:2012-09-09
8:2012-12-08

paste [选项] 文件1 文件2  (将文件黏贴到一起)
选项:
-d 设置新的分隔符,默认是空格或TAB
-s 将每个文件黏贴成一行
 从标准输入中读取数据

示例:
1、合并animal.txt和frut.txt以@作为分割符
[sleep@localhost greptest]$ paste -d@ animal.txt frut.txt
fish@pingguo:apple:12:lhwl:2012-11-01
cat@juzi:orange:10:lhzxt:2012-10-11
dog@juzi:orange:10:lhzxt:2012-10-11
bord@boluo:boluo:12:lhsjs:2012-11-11
tiger@juzi:orange:10:lhzxt:2012-10-11
@xiangjiao:banana:14:lhwl:2012-09-09
@shizi:shizi:8:herj:2012-12-08

2、合并animal.txt和frut.txt以@作为分割符,每个文件为一行
[sleep@localhost greptest]$ paste -sd@ animal.txt frut.txt
fish@cat@dog@bord@tiger
pingguo:apple:12:lhwl:2012-11-01@juzi:orange:10:lhzxt:2012-10-11@juzi:orange:10:lhzxt:2012-10-11@boluo:boluo:12:lhsjs:2012-11-11@juzi:orange:10:lhzxt:2012-10-11@xiangjiao:banana:14:lhwl:2012-09-09@shizi:shizi:8:herj:2012-12-08
[sleep@localhost greptest]$

split [选项] 待切割的大文件 输出的小文件(分割文本)
选项:
-或-1 指定切割成小文件的行数
-b    指定切割成小文件的字节
-C    与-b类似,切割时尽量维持每行的完整性

示例:
1、将animal每两行进行分割 默认“x”开头
[sleep@localhost greptest]$ split -2 animal.txt
[sleep@localhost greptest]$ ll
-rw-rw-r-- 1 sleep sleep    9 Oct 18 22:54 xaa
-rw-rw-r-- 1 sleep sleep    9 Oct 18 22:54 xab
-rw-rw-r-- 1 sleep sleep    6 Oct 18 22:54 xac

2、将frut.txt以100字符进行分割
[sleep@localhost greptest]$ split -b100 frut.txt
[sleep@localhost greptest]$ ll
-rw-rw-r-- 1 sleep sleep  100 Oct 18 22:57 xaa
-rw-rw-r-- 1 sleep sleep  100 Oct 18 22:57 xab
-rw-rw-r-- 1 sleep sleep   27 Oct 18 22:57 xac
[sleep@localhost greptest]$ split -C100 frut.txt
-rw-rw-r-- 1 sleep sleep   97 Oct 18 22:58 xaa
-rw-rw-r-- 1 sleep sleep  100 Oct 18 22:58 xab
-rw-rw-r-- 1 sleep sleep   30 Oct 18 22:58 xac

0 0
原创粉丝点击