Intro To 'shuf' Command In Linux
2023-06-06 - By Robert Elder
I use the 'shuf' command to generate random permutations of lines in a file:
shuf my-file.txt
Randomize Lines In A File
Here, I have a file called 'students.txt' that contains a sorted list of students in a class:
Charles
Christopher
David
Elizabeth
Jennifer
Jessica
Karen
Linda
Lisa
Mary
Michael
Patricia
Richard
Susan
William
I want to randomly rearrange the lines in this file to provide a seating assignment for each student. I can use the 'shuf' command to generate a new randomly rearranged list like this:
cat students.txt | shuf
Michael
Karen
David
Susan
Patricia
Mary
Lisa
Elizabeth
Richard
Linda
Jennifer
Charles
Christopher
Jessica
William
Select N Random Lines From A File Without Replacement
If I want to randomly select only three of the students from this list I can use the '-n' flag like this:
cat students.txt | shuf -n 3
Jennifer
Jessica
Karen
Select N Random Lines From A File With Replacement
By default, the 'shuf' command prints out each line only once. If you use the '-r' flag, all of the lines will be repeated an infinite number of times randomly so that duplicate lines can occur:
cat students.txt | shuf -r
Jessica
Michael
Michael
William
Richard
... Lines printed until you use Ctrl + C...
Using the 'head' command I can limit the output to the first 10 lines:
cat students.txt | shuf -r | head -n 10
Susan
Richard
Richard
Michael
Michael
Patricia
Karen
Michael
Jessica
Elizabeth
Notice how the output now contains duplicate lines (in this case, 'Richard' and 'Michael').
For another example use of the '-r' flag, I can create 15 random samples from the file 'key-strokes.txt':
KEY_UP
KEY_DOWN
KEY_LEFT
KEY_RIGHT
using the following command:
cat key-strokes.txt | shuf -r | head -n 15
KEY_LEFT
KEY_LEFT
KEY_DOWN
KEY_RIGHT
KEY_UP
KEY_DOWN
KEY_UP
KEY_RIGHT
KEY_LEFT
KEY_LEFT
KEY_UP
KEY_DOWN
KEY_UP
KEY_LEFT
KEY_DOWN
Generate Random Numbers Filling A Range
I can also use the '-i' flag to specify a number range and the output will produce random numbers filling this range:
shuf -i 1-10
9
3
5
6
10
1
7
2
8
4
shuf -i 92-106
96
105
98
93
94
104
103
106
92
102
95
101
100
97
99
And that's why the 'shuf' command is my favourite Linux command.
![]() Intro To 'stty' Command In Linux
Published 2023-10-04 |
![]() $1.00 CAD |
![]() Intro To 'nproc' Command In Linux
Published 2023-07-15 |
![]() Intro To 'comm' Command In Linux
Published 2023-09-06 |
![]() How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
![]() A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
![]() A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
![]() Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|