translated

This commit is contained in:
geekpi 2018-04-20 08:38:29 +08:00
parent e412f555df
commit 1f9e4cd91f
2 changed files with 166 additions and 168 deletions

View File

@ -1,168 +0,0 @@
translating---geekpi
The Shuf Command Tutorial With Examples For Beginners
======
![](https://www.ostechnix.com/wp-content/uploads/2018/04/shuf-command-720x340.png)
The Shuf command is used to generate random permutations in Unix-like operating systems. Using shuf command, we can shuffle the lines of a given input file randomly. Shuf command is part of GNU Coreutils, so you dont have bother with installation. In this brief tutorial, let me show you some examples of shuf command.
### The Shuf Command Tutorial With Examples
I have a file named **ostechnix.txt** with the following contents.
```
$ cat ostechnix.txt
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
```
Now let us display the above lines in a random order. To do so, run:
```
$ shuf ostechnix.txt
line2
line8
line5
line10
line7
line1
line4
line6
line9
line3
```
See? The above command randomized the order of lines in the file named “ostechnix.txt” and output the result.
You might want to write the output to another file. For example, I want to save the output in a file named **output.txt**. To do so, first create output.txt file:
```
$ touch output.txt
```
Then, write the output to that file using **-o** flag like below.
```
$ shuf ostechnix.txt -o output.txt
```
The above command will shuffle the contents of ostechnix.txt file randomly and write the output to output.txt file. You can view the output.txt file contents using command:
```
$ cat output.txt
line2
line8
line9
line10
line1
line3
line7
line6
line4
line5
```
I just want to display any one of the random line from the file. How do I do it? Simple!
```
$ shuf -n 1 ostechnix.txt
line6
```
Similarly, we can pick the first “n” random entries. The following command will display the only the first five random entries.
```
$ shuf -n 5 ostechnix.txt
line10
line4
line5
line9
line3
```
Instead of reading the lines from a file, we can directly pass the inputs using **-e** flag like below.
```
$ shuf -e line1 line2 line3 line4 line5
line1
line3
line5
line4
line2
```
You can pass shuffle the numbers too:
```
$ shuf -e 1 2 3 4 5
3
5
1
4
2
```
To quickly pick any one from the given range, use this command instead.
```
$ shuf -n 1 -e 1 2 3 4 5
```
Or, pick any three random numbers like below.
```
$ shuf -n 3 -e 1 2 3 4 5
3
5
1
```
We can also generate random numbers within a particular range. For example, to display random numbers between 1 to 10, simply use:
```
$ shuf -i 1-10
1
9
8
2
4
7
6
3
10
5
```
For more details, refer man pages.
```
$ man shuf
```
And, thats all for today. More good stuffs to come. Stay tuned!
Cheers!
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/the-shuf-command-tutorial-with-examples-for-beginners/
作者:[SK][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
选题:[lujun9972](https://github.com/lujun9972)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.ostechnix.com/author/sk/

View File

@ -0,0 +1,166 @@
给初学者看的 Shuf 命令教程
======
![](https://www.ostechnix.com/wp-content/uploads/2018/04/shuf-command-720x340.png)
Shuf 命令用于在类 Unix 操作系统中生成随机排列。使用 shuf 命令我们可以随机打乱给定输入文件的行。Shuf 命令是 GNU Coreutils 的一部分,因此你不必担心安装问题。在这个简短的教程中,让我向你展示一些 shuf 命令的例子。
### 带例子的 Shuf 命令教程
我有一个名为 **ostechnix.txt** 的文件,内容如下。
```
$ cat ostechnix.txt
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
```
现在让我们以随机顺序显示上面的行。为此,请运行:
```
$ shuf ostechnix.txt
line2
line8
line5
line10
line7
line1
line4
line6
line9
line3
```
看到了吗?上面的命令将名为 “ostechnix.txt” 中的行随机排列并输出了结果。
你可能想将输出写入另一个文件。例如,我想将输出保存到 **output.txt** 中。为此,请先创建 output.txt
```
$ touch output.txt
```
然后,像下面使用 **-o** 标志将输出写入该文件。
```
$ shuf ostechnix.txt -o output.txt
```
上面的命令将随机随机打乱 ostechnix.txt 的内容并将输出写入 output.txt。你可以使用命令查看 output.txt 的内容:
```
$ cat output.txt
line2
line8
line9
line10
line1
line3
line7
line6
line4
line5
```
我只想显示文件中的任意一行。我该怎么做?很简单!
```
$ shuf -n 1 ostechnix.txt
line6
```
同样,我们可以选择前 “n” 个随机条目。以下命令将只显示前五个随机条目。
```
$ shuf -n 5 ostechnix.txt
line10
line4
line5
line9
line3
```
如下所示,我们可以直接使用 **-e** 标志传入输入,而不是从文件中读取行。
```
$ shuf -e line1 line2 line3 line4 line5
line1
line3
line5
line4
line2
```
你也可以传入数字:
```
$ shuf -e 1 2 3 4 5
3
5
1
4
2
```
要快速在给定范围选择一个,请改用此命令。
```
$ shuf -n 1 -e 1 2 3 4 5
```
或者,选择下面的任意三个随机数字。
```
$ shuf -n 3 -e 1 2 3 4 5
3
5
1
```
我们也可以在特定范围内生成随机数。例如,要显示 1 到 10 之间的随机数,只需使用:
```
$ shuf -i 1-10
1
9
8
2
4
7
6
3
10
5
```
有关更多详细信息,请参阅手册页。
```
$ man shuf
```
今天就是这些。还有更多更好的东西。敬请关注!
干杯!
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/the-shuf-command-tutorial-with-examples-for-beginners/
作者:[SK][a]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
选题:[lujun9972](https://github.com/lujun9972)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.ostechnix.com/author/sk/