Shell numeric operation

Source: Internet
Author: User
1. Five methods for calculating the value:
(I ++ ))
Let I ++
I = $ (expr $ I + 1)
I = $ (echo $ I + 1 | BC)
I =$ (echo $ I 1 | awk '{printf $1 + $2 }')
 
2. The time command is used to count the command execution time, which includes the total running time and user space.
Execution time, kernel space execution time, which is implemented through the ptrace system call.
 
3. modulo % is used, while the power is only the ^ symbol used by BC. The symbols used by other commands are **.
 
4. The BC program is a calculator in Linux and can be used to calculate the hexadecimal conversion. For example:
Echo "obase: 10; ibase8; 11" | BC
Another method for processing hexadecimal conversions is:
Echo $(8 #20 ))
It indicates 20 of the octal values and the decimal value.
 
5. There are two floating point calculation methods:
* Using BC computing:
Echo "scale = 3; 1/13" | BC
If scale is not added, use the following format:
Echo "1/13" | BC-l
* Use awk computing:
Echo "1 3" | awk '{printf ("% 0.3f", $1 + $2 )}'
 
6. $ # indicates the number of parameters passed to the script, excluding the current executable file. For example:
./Test. Sh 1 # $ # is 1.
$? The Return Value of the previous command.
 
7. The environment variable random generates a random number between 0 and 32767. The rand function of awk generates a random number between 0 and 1.
When using the rand function of awk, you must first use the srand function to bury the seeds. For example:
Echo "" | awk '{srand (); printf ("% F", Rand ());}'
 
8. seq can generate a series of numbers, which can increase progressively according to the specified interval. For example:
SEQ 5 # generate 5 to 5 digits
SEQ 1 2 6 # generate Series 1 3 5
SEQ-S: 1 5 # generate Series 1: 2: 3: 4: 5
SEQ-W 1 100 # generate series 001 002... 100
SEQ-F "0x % G" 1 3 # generate series 0x1 0x2 0x3
 
9. How to Use SED
* Cat regular_express.txt | SED's/[^ A-Za-Z]/\ n/G' | \
Sed '/^ $/d' | sort | uniq-c | sort-K 1-n-r | HEAD
The preceding command is used to count the top 10 words that appear the most frequently in the text,
Sed's/[^ A-Za-Z]/\ n/G' is used to separate words, and sed '/^ $/d' is used to remove empty rows,
Sort sorts separated words so that the same words are arranged together.
Use uniq-C to calculate the number of times each word appears.
Words are sorted by the number of times they appear. The head only extracts the top 10 words.
* Cat index.html | sed-E's/[^ A-Za-Z]/\ n/G' | sed '/^ $/d' | \
Grep '^ The $' | uniq-c | SED's/[^ 0-9] // G'
Find the number of occurrences
 
10. Shift moves the command line parameter to the left each time, so the current parameter to be processed is always $1


References

====================

Shell programming example -- by Falcon

Shell numeric operation

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.