Preface I haven't written a shell script for a long time. Today I can see such a requirement in the group as follows: I also reviewed the shell script and wrote it easily. ProgramAfter testing, I basically passed the train of thought.
- First, use shell scripts to read text by line and store each row in an array.
- Select a random number each time to retrieve data from the array.
- Note that it cannot be repeated rows, so the random number has a marked field.
Shell script
#! /Bin/bash # File Location = ". /test.txt "# counter I = 0 # Read the file content by row and store it in the ARR array for line in $ (awk '{print $0}' $ file) doarr [$ I] = $ linei = $ (expr $ I + 1) Done # construct a random number array. Assume that the file contains only 100 rows, when Random [I] = 0, it indicates that row I is not read for I in $ (SEQ 1 100) dorandom [$ I] = 0 done # construct a random number, randomly select 25 rows for (I = 0; I <25;) doindex = $ random % 100if [$ {random [$ Index]}-EQ 0]; thenrandom [$ Index] = 1 echo $ {arr [$ Index]} I =$ (expr $ I + 1) fidone
Note: you are welcome to discuss it with the post. If you are familiar with the language, you will not be unfamiliar with it!