shuf命令简单用法
背景:需要将一个文件的内容按照行进行打乱顺序;需要从一个大文件中随机获取多少行的内容,除了python脚本处理以外,一些shell命令也可以更快捷的做到
shuf命令详解
bash-4.1$ shuf --help
Usage: shuf [OPTION]... [FILE]
or: shuf -e [OPTION]... [ARG]...
or: shuf -i LO-HI [OPTION]...
Write a random permutation of the input lines to standard output.
Mandatory arguments to long options are mandatory for short options too.
-e, --echo treat each ARG as an input line
-i, --input-range=LO-HI treat each number LO through HI as an input line
-n, --head-count=COUNT output at most COUNT lines
-o, --output=FILE write result to FILE instead of standard output
--random-source=FILE get random bytes from FILE
-z, --zero-terminated end lines with 0 byte, not newline
--help display this help and exit
--version output version information and exit
With no FILE, or when FILE is -, read standard input.
Report shuf bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'shuf invocation'
# 例1:将一个文件顺序打乱输出到另一个文件
shuf a.txt > b.txt
shuf a.txt -o b.txt # 以文件格式输出到b
# 例2:将一个文件中的数据随机获取50条数据存到另一个文件中
shuf a.txt -o b.txt -n 30