Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the shuf command in linux

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the shuf command in linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The shuf command is used to generate random permutations in Unix-like operating systems. Using the shuf command, we can randomly scramble the lines of a given input file. The shuf command is part of GNU Coreutils, so you don't have to worry about installation.

Shuf command tutorial with examples

I have a file called ostechnix.txt, which reads as follows:

$cat ostechnix.txtline1line2line3line4line5line6line7line8line9line10

Now let's display the above lines in random order. To do this, run:

$shuf ostechnix.txtline2line8line5line10line7line1line4line6line9line3

You see that? The above command randomly arranges the rows named ostechnix.txt and outputs the results.

You may want to write the output to another file. For example, I want to save the output to output.txt. To do this, first create an output.txt:

$touch output.txt

Then, use the-o flag as follows to write the output to the file:

$shuf ostechnix.txt-o output.txt

The above command randomly disrupts the contents of the ostechnix.txt and writes the output to the output.txt. You can use the command to view the contents of output.txt:

$cat output.txt line2line8line9line10line1line3line7line6line4line5

I just want to display any line in the file. What should I do? It's simple!

$shuf-n 1 ostechnix.txtline6

Similarly, we can select the first "n" random entry. The following command displays only the first five random entries:

$shuf-n 5 ostechnix.txtline10line4line5line9line3

As shown below, we can directly use the-e flag to pass in the input instead of reading lines from the file:

$shuf-e line1 line2 line3 line4 line5line1line3line5line4line2

You can also pass in numbers:

$shuf-e 1 2 3 4 535142

To quickly select one in a given range, use this command instead:

$shuf-n 1-e 1 2 3 4 5

Or, select any three random numbers below:

$shuf-n 3-e 1 2 3 4 5351

We can also generate random numbers within a specific range. For example, to display a random number between 1 and 10, simply use:

$shuf-I 1-1019824763105

For more details, see the man pages.

This is the end of $man shuf's article on "how to use shuf commands in linux". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report