Shell我用 LinuxLinux学习之路

使用Shell重新排序文件行

2017-04-07  本文已影响28人  wyaoo

测试中需要把有序排列的文件打乱的需求。于是用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
上一篇下一篇

猜你喜欢

热点阅读