Numerical calculations in the shell

Source: Internet
Author: User
Tags alphabetic character builtin rand

#!/bin/bashecho "Please input number:" Read na= ' expr $n/100 ' #a1 = ' expr $n-$a * b= ' echo ' ($n-$a *100)/10 "| BC ' c= ' echo ' ($n-$a *100-$b *10) "| BC '  d= ' echo ' $c *100+ $b *10+ $a "| BC ' echo $d turn: http://bbs.techrepublic.com.cn/viewthread.php?tid=1404251. Add a $ i=0;$ ((i++)) $ echo $i 1$ let i++$ echo $ for a number i2$ expr $i + 13$ echo $i 2$ echo $i 1 | awk ' {printf $1+$2} '  let i++;i=$ (expr $i + 1) i=$ (echo $i +1|BC) i=$ (echo "$i 1" | awk ' {printf $1+$2;} ')   2. Time statistics Script Runtime $ calc.sh10000 real    0m1.319suser     0m1.056ssys     0m0.036s$ time Calc_let.sh3. Use the shell's built-in commands to see the types of commands as follows: $ type typetype is a shell builtin$ type Letlet is a shell builtin$ type exprexpr are hashed (/usr/bin/expr) $ type BCBC is hashed (/USR/BIN/BC) $ type Awkawk IS/USR/BIN/AWK4.LET,EXPR,BC can be used for modulo, operators are%, and let and BC can be used to exponentiation, operators are different, the former is * *, the latter is ^//modulo $ expr 5 21$ let i=5%2$ echo $i 1$ Echo 5% 2 | bc1$ ((i=5%2) $ echo $i 1//exponentiation $ let i=5**2$ echo $i 25$ ((i=5**2)) $ echo $i 25$ ECho "5^2" | Bc25 5. Floating-point budgeting, let and expr cannot perform floating-point operations, but BC and awk can. Example one:    $ echo "scale=3; 1/13 " | bc    .076   $ echo "1 13" | awk ' {printf ("%0.3f\n", $1/$2)} '     0.077 Example II: $ echo 1/13100 | bc-l.00007633587786259541  example Three: if there is such a set of data, the number of households in a village and the total monthly income, the need to find the highest per capita monthly income families. File name Innode1 3 44902 5 38963 4 31124 4 47165 4 45786 6 53997 3 50898 6 30299 4 619510 5 5145 Note: The following script produces the above three columns of random numbers: For i in $ (SE Q 1);d o echo $i $ (($RANDOM/8192+3)) $ ((random/10+3000));d one  Description: The three columns above are family number, family size, household monthly income. Analysis: In order to find the family with the highest monthly income, we need to divide the next two columns, that is, the average monthly income of each family, and then according to the average monthly income to identify the highest income families.  #!/bin/bash# gettopfamily.sh[$#-lt 1] && echo "Please input the file who store the income data" &&amp ; Exit-1[!-F $] && echo "is not a file" && Exit-1income=$1awk ' {     &nbs p; printf ("%d%0.2f\n", $ $, $3/$2);} ' $income | Sort-k 2-n-r 6. Generates a random number environment variable random generates a number from 0 to 32767, while the RAND function of awk can produce random numbers from 0 to 1Number. Example one: $ echo $RANDOM 81$ echo "" | awk ' {srand (); printf ("%f", Rand ());} ' 0.237788 Description: Srand in the absence of parameters, the current time is used as a seed 7 of the rand random number generator. Count the number of words in an article: Cat text | Sed-e "S/[^a-za-z]/\n/g" | Grep-v ^$ | Sort | Uniq-c 8. Statistics the top 10 most frequently occurring words cat text | Sed-e "S/[^a-za-z]/\n/g" | Grep-v ^$ | Sort | uniq-c | Sort-n-K 1-r | head-10  Description: Cat text: Displays the contents of the text file Sed-e "s/[^a-za-z]/\n/g": Replace all non-alphabetic characters with spaces so that the entire text is left with an alphabetic character grep-v ^$: Remove the empty line sort: Sort Uniq-c: Count the number of peers, that is, the number of each word sort-n-K 1-r: Sort by the number (-N) in the first column (-K 1) in reverse order (-R) head-10: Remove the first 10 lines 9. The number of specified words in the statistics article code one #!/bin/bash# Statistic_words.shif [$#-lt 1]; Then        echo "error:you should input 2 words at least";        echo "Usage:basename $ FILE WORDS ...."        exit-1fifile=$1 ((words_num=$#-1)) for n in $ (seq $WORDS _num) do    shift    cat $ FILE | Sed-e "S/[^a-za-z]/\n/g" | Grep-v ^$ | Sort | grep ^$1$ | Uniq-cdone Code two #!/bin/bash# Statistic_words.shif [$#-lt 1]; Then        echo "error:you should input 2 words at least";        echo "Usage:basename $ FILE WORDS ...."         exit-1fifile=$1 ((words_num=$#-1)) for n in $ (seq $WORDS _num) do    shift     Cat $FILE | Sed-e "S/[^a-za-z]/\n/g" | Grep-v ^$ | Sort | uniq-c | grep "$1$" done  Description: It is clear that the first approach is much more efficient because the first method finds the words that need to be counted in advance and then counts them, while the latter is not. In fact, if you use the-e option of grep, we don't need to introduce loops, and we can do it with a single command: $ cat text | Sed-e "S/[^a-za-z]/\n/g" | Grep-v ^$ | Sort | Grep-e "^action$|^is$" | Uniq-c or cat text | Sed-e "S/[^a-za-z]/\n/g" | Grep-v ^$ | Sort | Egrep   "^action$|^is$" | uniq-c  added: In the Advanced Bash Scripting Programming Guide, the Jot Command and Factor command factor produce a number of all primes

Numerical calculation in the shell

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.