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

前端 未结 11 1022
醉酒成梦
醉酒成梦 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:32

    cat yourfile.txt | while IFS= read -r f; do printf "%05d %s\n" "$RANDOM" "$f"; done | sort -n | cut -c7-
    

    Read the file, prepend every line with a random number, sort the file on those random prefixes, cut the prefixes afterwards. One-liner which should work in any semi-modern shell.

    EDIT: incorporated Richard Hansen's remarks.

提交回复
热议问题