使用Shell重新排序文件行

作者: wyaoo | 来源:发表于2017-04-07 10:22 被阅读28次

    测试中需要把有序排列的文件打乱的需求。于是用shell的awk实现了一个小例子。

    最终命令:

    more input.txt|awk '{print rand()*1000000"|"$0}'|sort|awk -F"|" '{print $2}' >output.txt
    

    测试命令:

    TT183:~/wyao # more random0.txt
    1003000E004A340F
    0203000E004A3432
    0033000E0049E5D4
    0003000E0049E5EA
    TT183:~/wyao # awk '{print rand()*1000000"|"$0}' random0.txt >random1.txt
    TT183:~/wyao # more random1.txt
    237788|1003000E004A340F
    291066|0203000E004A3432
    845814|0033000E0049E5D4
    152208|0003000E0049E5EA
    TT183:~/wyao # sort random1.txt >random2.txt
    TT183:~/wyao # more random2.txt
    152208|0003000E0049E5EA
    237788|1003000E004A340F
    291066|0203000E004A3432
    845814|0033000E0049E5D4
    TT183:~/wyao # awk -F"|" '{print $2}' random2.txt
    0003000E0049E5EA
    1003000E004A340F
    0203000E004A3432
    0033000E0049E5D4
    

    相关文章

      网友评论

        本文标题:使用Shell重新排序文件行

        本文链接:https://www.haomeiwen.com/subject/wvacattx.html