从具有awk代码的文件中选择随机的3000行

use*_*487 5 random awk lines

我想从sample.file中随机选择3000行,其中包含8000行.我将使用awk代码或从命令行执行此操作.我怎样才能做到这一点?

Mor*_*ten 11

如果你有gnu排序,很容易:

sort -R FILE | head -n3000
Run Code Online (Sandbox Code Playgroud)

如果你有gnu shuf,那就更容易了:

shuf -n3000 FILE
Run Code Online (Sandbox Code Playgroud)


Ken*_*ent 5

awk 'BEGIN{srand();}
{a[NR]=$0}
END{for(i=1; i<=3000; i++){x=int(rand()*NR) + 1; print a[x];}}' yourFile
Run Code Online (Sandbox Code Playgroud)


Joe*_*oey 0

在 PowerShell 中:

Get-Content myfile | Get-Random -Count 3000
Run Code Online (Sandbox Code Playgroud)

或更短:

gc myfile | random -c 3000
Run Code Online (Sandbox Code Playgroud)