linux - 从一个单位为 2 行的文件中随机选择(单位)。

标签 linux bash shell sorting text-processing

我想从文件中选择随机行/单位,但单位由 2 行组成。

例如一个文件看起来像这样

Adam
Apple
Mindy
Candy
Steve
Chips
David
Meat
Carol
Carrots

我想随机子选择假设 2 个单元组

例如

Adam
Apple
David
Meat

Steve
Chips
Carol
Carrots

我试过使用 shufsort -R 但它们只随机播放 1 行。有人可以帮我吗? 谢谢。

最佳答案

你可以用 shuf 来做到这一点,方法是在洗牌之前加入行(如果这些行描述的是单个项目,这对于一般的文件格式来说可能不是一个坏主意):

$ < file sed -e 'N;s/\n/:/' | shuf | head -1 | tr ':' '\n'
Carol
Carrots

sed 一次加载两行,并用冒号连接它们。

关于linux - 从一个单位为 2 行的文件中随机选择(单位)。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44531880/

相关文章:

linux - 如何使用 sh 回显双反斜杠加上变量(版本号),如 "\\hostname\release\1.02A01"?

c++ - 如何在 Windows 上将 GoogleTest 变量 GTEST_LIBRARY GTEST_INCLUDE_DIR 和 GTEST_MAIN_LIBRARY 设置为 CMake?

bash - 在 bash 中用另一个文件替换整个文件

bash - 使用 imagemagick 的宽度和高度识别

file - 如果文件不存在,如何执行某些命令?

Linux 定时任务 : doing no effect

linux - “dotted” linestyle 的性能比 “dashed” linestyle 慢很多是正常的吗?

bash - 在 bash 中,如何将功能键绑定(bind)到命令?

bash - kubectl exec命令未记录在pod的bash历史记录中

r - 如何从 R 中运行多命令 Linux shell 脚本?