Linux的shuf命令

shuf 随机排序

待扩展->Linux下的随机数生成和排序方法,包含各类shell和sort

有时候在Linux中我们需要各种各样符合要求的的随机数,最主要和最常用做法是写个shell,利用 bash 里的内置变量 $RANDOM 进行排序,然后打印。
【细节待补充】

1.安装

针对需求简单的随机数,可以用shuf来做。它是系统本身专职干这事儿的,比shell高效。以下是使用要点:
这个命令属于 coreutils 包,一般系统自带,直接用就行。如果系统没有此命令,可以用

yum install coreutils

brew install coreutils

apt install coreutils

进行安装。

2.详情
它能够接受标准输入,即管道。

man页给出的使用方法如下:

   NAME
   shuf - generate random permutations
   SYNOPSIS
   shuf [OPTION]... [FILE]
   shuf -e [OPTION]... [ARG]...
   shuf -i LO-HI [OPTION]...

   DESCRIPTION
   Write a random permutation of the input lines to standard output.

   With no FILE, or when FILE is -, read standard input.

   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

   -r, --repeat
          output lines can be repeated

   -z, --zero-terminated
          line delimiter is NUL, not newline

   --help display this help and exit

   --version
          output version information and exit

其中常用参数:

      -i 写入范围
      -n 最多显示多少行
      -o 将随机排序后的输出写到一个文件中

3.例子
假设需要生成3个范围为0-200的随机数到文件rand.txt中,只需要如下操作即可,效率:

shuf -i 0-200 -n 3 -o rand.txt

当然这个也是有不足之处的,比如,它并不是严格意义上的随机数。但是可以满足一些非专业和严格要求的需要。【具体细节待补充】

参考来源:

  1. shuf 命令:随机排序文件:https://blog.csdn.net/reyleon/article/details/14000997
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值