Shell scrambled file line

Source: Internet
Author: User

idea, produce a random array, and then press the elements of the array to re-output the rows in the file 1, the generation of random array reading when it feels very simple.    The first time with a little bit.        The first is the definition, and the variables in the shell are weak variables that can be defined at any time. Arr= (' seq 57 ') creates a 1:        An array of 57, note that you must add parentheses, otherwise you cannot define an array.        You can also use a for loop to dynamically assign a value to an array. For ((i=0;i<57;i++)) #有空格 does arr[$i]=$ (($i + 1)) done again is to generate a random number using an internal variable $random, resulting in 0-327    67.    #!/bin/bash arr= (' seq ") let" a= $RANDOM%57 "let" b= $RANDOM%57 "echo" a= "$a echo" b= "$b tmp=${arr[$a]} arr[$a]=${arr[$b]} #赋值的时候arr [$a], do not write ${arr[$a]} arr[$b]= $tmp echo ${arr[@]} cycle the above process 10,000 times 44 13 48 53 22 15 35 28 1 31 8 49 4 34 6 55 20 32 26 40 18 37 16 43 9 27 25 42 2 3 50 11 56 46 24 41 7 12 54 30 45 47 17 23 33 10 51 19 52 38 57 14 29 36 39 5 212, scrambled file line #!/bin/bash arr= (' seq ') for ((i=0;i<10000;i++)) do le T "a= $RANDOM%57" let "b= $RANDOM%57" tmp=${arr[$a]} arr[$a]=${arr[$b]} arr[$b]=  $tmp done #echo ${arr[@]} for I in ${arr[@]} do          J=0 while read line #注意读取文件的写法 do (j + +) If ["$i"                             -eq "$j"];then echo "i= $i, j= $j: $line" echo $line >> list1.txt Break fi do < backup.txt done through the above script, feel even after reading the book, know some of the east West, or not, the important thing is to keep practicing. Even if it is a simple thing, such as an array, if judgment, and so on, can only be above his business when not written.

Shell scrambled file line

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.