文件排序: sort命令是帮我们根据不同的数据类型进行排序。其语法及经常使用參数格式: sort [选项][输入文件] 补充说明:sort可针对文本文件的内容,以行为单位来排序。 參 数: -b 忽略每行前面開始出的空格字符。 -c 检查文件是否已经依照顺序排序。 -f 排序时。忽略大写和小写字母。 -M 将前面3个字母按照月份的缩写进行排序。 -n 按照数值的大小排序。 -o<输出文件> 将排序后的结果存入指定的文件。 -r 以相反的顺序来排序。 -t<分隔字符> 指定排序时所用的栏位分隔字符。 -k 选择以哪个区间进行排序 编写cargo.db文件
china:121:232:NE3453usa:434:435:SS343Hongkong:2323:343:KO32china:9034:234:HU423china:9032:5443:IJ2321.以默认方式排序:
[root@iZ2546h6zurZ test]# sort -t: cargo.db china:121:232:NE3453china:9032:5443:IJ232china:9034:234:HU423Hongkong:2323:343:KO32usa:434:435:SS343当中使用-t改动分隔符为: 2.指定依照某个域进行排序(-k)
[root@iZ2546h6zurZ test]# sort -t: -k2 cargo.db china:121:232:NE3453Hongkong:2323:343:KO32usa:434:435:SS343china:9032:5443:IJ232china:9034:234:HU423以上排序不能依照数字进行排序 3.依照数字大小排序(-n)
[root@iZ2546h6zurZ test]# sort -t: -k2n cargo.db china:121:232:NE3453usa:434:435:SS343Hongkong:2323:343:KO32china:9032:5443:IJ232china:9034:234:HU4234.将排序后的文件重定向到还有一个文件里(-o)
[root@iZ2546h6zurZ test]# sort -t: -k2n -o cargo2.db cargo.db5.awk和sort结合使用 编辑文件location.db
bei jingqwwqfdfdfdfaji nansfdfsdfdfdsfdcheng dugfgadffsdfwdfwhang zhoufsdfsffsdgsd按块进行排序
[root@iZ2546h6zurZ test]# cat location.db |awk -v RS="" '{gsub("\n","@");print}' | sort | awk -v ORS="\n\n" '{gsub("@","\n");print}'bei jingqwwqfdfdfdfacheng dugfgadffsdfwdfwhang zhoufsdfsffsdgsdji nansfdfsdfdfdsfd当中awk -v 就是BEGIN块 RS表示记录的切割符;ORS表示输出分隔符 6.去除反复行 uniq命令 编辑文件location2.db
hahahahhahahahlcqhellohahahahlcqworld
[root@iZ2546h6zurZ test]# uniq location2.db hahahahlcqhellohahahahlcqworld
[root@iZ2546h6zurZ test]# sort -u location2.db hahahahhellolcqworlduniq命令去除相邻的反复行,sort -u去除后面全部的反复行。 7.记录连接命令join,保证两个文件是有序的 编辑文件 stu2.db
lcq:stu:hahahsgf:stu:dsiwewxm:stu:2e2ds文件stu2_body.db
lcq:fsdfssgf:fvbdfgdgfgfgfxm:fsdfsd
[root@iZ2546h6zurZ test]# join -t: stu2.db stu2_body.dblcq:stu:hahah:fsdfssgf:stu:dsiwew:fvbdfgdgfgfgfxm:stu:2e2ds:fsdfsd8. cut命令按域或者行提取文本
[root@iZ2546h6zurZ test]# cut -d: -f1,2 stu2.dblcq:stusgf:stuxm:stu当中-d改变域分隔符,-f指定提取的域 9.压缩文件和解压文件
[root@iZ2546h6zurZ test]# tar -cf stu.all stu*将stu开头的文件压缩为stu.all 解压文件
[root@iZ2546h6zurZ test]# tar -xvf stu.all以上是解压非gzip文件
[root@iZ2546h6zurZ test]# gzip stu.all将stu.all压缩为stu.all.gz
[root@iZ2546h6zurZ test]# tar -zxvf stu.all.gz stu2_body.dbstu2.dbstu_body.dbstu.db加上-z解压gzip文件