Search code examples
linuxfilerandomredhatshuffle

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


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 perl or awk one-liner that accomplishes the same task.


Solution

  • And a Perl one-liner you get!

    perl -MList::Util -e 'print List::Util::shuffle <>'
    

    It uses a module, but the module is part of the Perl code distribution. If that's not good enough, you may consider rolling your own.

    I tried using this with the -i flag ("edit-in-place") to have it edit the file. The documentation suggests it should work, but it doesn't. It still displays the shuffled file to stdout, but this time it deletes the original. I suggest you don't use it.

    Consider a shell script:

    #!/bin/sh
    
    if [[ $# -eq 0 ]]
    then
      echo "Usage: $0 [file ...]"
      exit 1
    fi
    
    for i in "$@"
    do
      perl -MList::Util -e 'print List::Util::shuffle <>' $i > $i.new
      if [[ `wc -c $i` -eq `wc -c $i.new` ]]
      then
        mv $i.new $i
      else
        echo "Error for file $i!"
      fi
    done
    

    Untested, but hopefully works.