zoukankan      html  css  js  c++  java
  • 文件排序合并

    文件排序:

    sort命令是帮我们根据不同的数据类型进行排序。其语法及经常使用參数格式:

      sort [选项][输入文件]

    补充说明:sort可针对文本文件的内容,以行为单位来排序。

    參  数:
    -b 忽略每行前面開始出的空格字符。
    -c 检查文件是否已经依照顺序排序。
    -f 排序时。忽略大写和小写字母。


    -M 将前面3个字母按照月份的缩写进行排序。
    -n 按照数值的大小排序。
    -o<输出文件> 将排序后的结果存入指定的文件。


    -r 以相反的顺序来排序。
    -t<分隔字符> 指定排序时所用的栏位分隔字符。
    -k 选择以哪个区间进行排序

    编写cargo.db文件

    china:121:232:NE3453
    usa:434:435:SS343
    Hongkong:2323:343:KO32
    china:9034:234:HU423
    china:9032:5443:IJ232

    1.以默认方式排序:
    [root@iZ2546h6zurZ test]# sort -t: cargo.db 
    china:121:232:NE3453
    china:9032:5443:IJ232
    china:9034:234:HU423
    Hongkong:2323:343:KO32
    usa:434:435:SS343

    当中使用-t改动分隔符为:
    2.指定依照某个域进行排序(-k)
    [root@iZ2546h6zurZ test]# sort -t: -k2 cargo.db 
    china:121:232:NE3453
    Hongkong:2323:343:KO32
    usa:434:435:SS343
    china:9032:5443:IJ232
    china:9034:234:HU423

    以上排序不能依照数字进行排序
    3.依照数字大小排序(-n)
    [root@iZ2546h6zurZ test]# sort -t: -k2n cargo.db 
    china:121:232:NE3453
    usa:434:435:SS343
    Hongkong:2323:343:KO32
    china:9032:5443:IJ232
    china:9034:234:HU423

    4.将排序后的文件重定向到还有一个文件里(-o)
    [root@iZ2546h6zurZ test]# sort -t: -k2n -o cargo2.db cargo.db 

    5.awk和sort结合使用
    编辑文件location.db
    bei jing
    qwwq
    fdfdfdfa
    
    
    ji nan
    sfdfs
    dfdfdsfd
    
    
    cheng du
    gfgadf
    fsdfwdfw
    
    
    hang zhou
    fsdfsf
    fsdgsd

    按块进行排序

    [root@iZ2546h6zurZ test]# cat location.db |awk -v RS="" '{gsub("
    ","@");print}' | sort | awk -v ORS="
    
    " '{gsub("@","
    ");print}'
    bei jing
    qwwq
    fdfdfdfa
    
    
    cheng du
    gfgadf
    fsdfwdfw
    
    
    hang zhou
    fsdfsf
    fsdgsd
    
    
    ji nan
    sfdfs
    dfdfdsfd

    当中awk -v 就是BEGIN块

    RS表示记录的切割符;ORS表示输出分隔符


    6.去除反复行
    uniq命令
    编辑文件location2.db
    hahahah
    hahahah
    lcq
    hello
    hahahah
    lcq
    world

    [root@iZ2546h6zurZ test]# uniq location2.db 
    hahahah
    lcq
    hello
    hahahah
    lcq
    world

    [root@iZ2546h6zurZ test]# sort -u location2.db 
    hahahah
    hello
    lcq
    world

    uniq命令去除相邻的反复行,sort -u去除后面全部的反复行。

    7.记录连接命令join,保证两个文件是有序的
    编辑文件
    stu2.db

    lcq:stu:hahah
    sgf:stu:dsiwew
    xm:stu:2e2ds

    文件stu2_body.db
    lcq:fsdfs
    sgf:fvbdfgdgfgfgf
    xm:fsdfsd

    [root@iZ2546h6zurZ test]# join -t: stu2.db stu2_body.db
    lcq:stu:hahah:fsdfs
    sgf:stu:dsiwew:fvbdfgdgfgfgf
    xm:stu:2e2ds:fsdfsd

    8. cut命令按域或者行提取文本

    [root@iZ2546h6zurZ test]# cut -d: -f1,2 stu2.db
    lcq:stu
    sgf:stu
    xm: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.db
    stu2.db
    stu_body.db
    stu.db
    

    加上-z解压gzip文件

  • 相关阅读:
    国外可用的谷歌地图(可根据地址搜索经纬度)
    后台css框架(自用)
    DBHelp类sql分页(自用笔记)
    定制C++高效安全的运行时动态类型转换
    C++11右值引用和std::move语句实例解析
    浏览器内核-Webkit
    获取股票历史数据和当前数据的API
    从浏览器启动应用程序
    一个实时获取股票数据的安卓应用程序
    C++数据类型总结
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6790575.html
Copyright © 2011-2022 走看看