其他命令 · 2022年2月8日

shuf命令

shuf命令相关的命令是:sort

shuf命令把输入行按随机顺序输出到标准输出。

用法

shuf [选项]… [文件]

或者

shuf -e [选项]… [参数]…

又或者

shuf -i LO-HI [选项]…

常用参数

-e, –echo
将每个参数视为输入行

-i, –input-range=LO-HI
将LO 到HI 的每个数字视为输入行

-n, –head-count=行数
最多输出指定的行数

-o, –output=文件
将结果输出到指定文件而非标准输出

–random-source=文件
从指定文件获得随机比特

-z, –zero-terminated
以0 结束行而非新行

–help
显示此帮助信息并退出

–version
显示版本信息并退出

如果没有指定文件,或者文件为”-“,则从标准输入读取。

实例1

[tank@localhost test1]# cat 1.txt       #有序
1
2
3
4
5
6
[tank@localhost test1]# shuf 1.txt      #无序
6
2
3
5
4
1

实例2

[root@pf]# shuf -e 1 2 3 4 5 6
1
2
6
4
3
5

[root@pf]# shuf -i 1-6
6
5
4
2
3
1

[root@pf]# shuf -n3 -i 1-6
4
5
1

[root@pf]# shuf -n3 -i 1-6 -o out.txt

[root@pf]# cat out.txt
5
4
6