How to use the command 'shuf' (with examples)

How to use the command 'shuf' (with examples)

The ‘shuf’ command is a utility that generates random permutations. It can be used to randomize the order of lines in a file, generate random numbers within a specified range, and more.

Use case 1: Randomize the order of lines in a file and output the result

Code:

shuf path/to/file

Motivation: This use case is helpful when you want to shuffle the contents of a file randomly. For example, if you have a list of names in a file and want to randomly assign them to groups, you can use ‘shuf’ to randomize the order of lines in the file.

Explanation: The ‘shuf’ command is followed by the path to the input file. It will read the file, randomize the order of lines, and output the result to the console.

Example Output:

Jane
John
Mary
David

Use case 2: Only output the first 5 entries of the result

Code:

shuf --head-count=5 path/to/file

Motivation: This use case is useful when you only need a subset of the randomized lines. For example, if you have a large dataset and only need a smaller sample for testing or analysis purposes, you can use ‘shuf’ to randomly select a limited number of entries.

Explanation: The ‘–head-count’ option specifies the number of lines to output from the randomized result. In this case, we set it to 5. The ‘path/to/file’ argument is the path to the input file.

Example Output:

Jane
David
Alex
Samantha
John

Use case 3: Write the output to another file

Code:

shuf path/to/input --output=path/to/output

Motivation: This use case is useful when you want to save the randomized result to a file for future reference or further processing. For example, if you have a questionnaire with multiple choice questions and want to randomize the order of the options, you can use ‘shuf’ to generate the randomized result and save it to a new file.

Explanation: The ‘path/to/input’ argument is the path to the input file. The ‘–output’ option followed by ‘path/to/output’ specifies the file where the randomized result will be saved.

Example Output: The output will be written to ‘path/to/output’ file.

Use case 4: Generate 3 random numbers in the range 1-10 (inclusive)

Code:

shuf --head-count=3 --input-range=1-10 --repeat

Motivation: This use case is helpful when you need to generate random numbers within a specific range. For example, if you want to simulate dice rolls or randomize the selection of winners from a pool, you can use ‘shuf’ to generate random numbers.

Explanation: The ‘–head-count’ option specifies the number of random numbers to generate. In this case, we set it to 3. The ‘–input-range’ option followed by ‘1-10’ specifies the range of numbers to generate. The ‘–repeat’ option allows for the repetition of numbers in the output.

Example Output:

5
3
7

Conclusion:

The ‘shuf’ command is a versatile tool for generating random permutations. With its various options, you can easily randomize the order of lines in a file, generate random numbers within a specified range, and more. It is a useful utility for tasks that require randomization or sampling.

Related Posts

How to use the command bspc (with examples)

How to use the command bspc (with examples)

The bspc command is a tool used to control bspwm, a tiling window manager for X11.

Read More
How to use the command microcom (with examples)

How to use the command microcom (with examples)

Microcom is a minimalistic terminal program that allows users to access remote devices via a serial, CAN or telnet connection from the console.

Read More
How to use the command 'dexter' (with examples)

How to use the command 'dexter' (with examples)

Dexter is a tool that allows authentication of kubectl users with OpenId Connect.

Read More