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