gpt4 book ai didi

linux - linux中随机选择行而不重复

转载 作者:太空宇宙 更新时间:2023-11-04 05:11:25 24 4
gpt4 key购买 nike

拜托,我需要帮助,因为我是 Linux 新手。我有一个包含数千行重复项的文件。我想随机选择该文件中的行(基于第一列)而不重复任何行。例如,我有一个这样的文件;

233  7
233 8
244 9
244 8
244 7
255 5
288 6
300 4
300 5

我想要的输出是;

233  7
244 8
255 5
288 6
300 4

我已经使用了shuf -n N file > newfile,但它重复了行。请帮助了解使用什么代码。

最佳答案

what i would want is that the selection is not limited to the row that comes first. i want it in such a way that it could pick the first in some instance and also the second in another instance.

我将按照我的理解重新表述您的要求:您有一个 2 列输入文件,如您的问题所示,在具有重复值的第一列上排序。您希望输出的第一列的每个唯一值都包含一个随机行,该行具有来自输入的该值。

awk命令通过简单地将输入行按第一列值分组提供给shuf -n1不同实例来生成此内容:

awk '$1 != c { close("shuf -n1"); c = $1 } { print|"shuf -n1" }' file

关于linux - linux中随机选择行而不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54872003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com