How can I randomize the lines in a file using standard tools on Red Hat Linux?

前端 未结 11 1039
醉酒成梦
醉酒成梦 2020-11-28 05:25

How can I randomize the lines in a file using standard tools on Red Hat Linux?

I don\'t have the shuf command, so I am looking for something like a

11条回答
  •  甜味超标
    2020-11-28 05:27

    A one-liner for python:

    python -c "import random, sys; lines = open(sys.argv[1]).readlines(); random.shuffle(lines); print ''.join(lines)," myFile
    

    And for printing just a single random line:

    python -c "import random, sys; print random.choice(open(sys.argv[1]).readlines())," myFile
    

    But see this post for the drawbacks of python's random.shuffle(). It won't work well with many (more than 2080) elements.

提交回复
热议问题