I recently wrote a shell script, which has high efficiency requirements, so I read some articles. I feel that this article is really good. The example in this article is really great! Institute
Use me to extract them:
@ 1: instance:
Requirement: calculates the cumulative result from 1 to 100000.
Method 1: Use bash shell for Numerical Calculation
# Time for (I = 0; I <= 100000; I ++); Do (sum + = I); done; echo $ sum
Real 0m1. 134 s
User 0m1. 080 s
Sys 0m0. 048 s
5000050000
Method 2: Use the value of awk for Calculation
# Time awk 'in in {While (I ++ <100000) sum + = I; printf "% d", sum ;}'
5000050000
Real 0m0. 029 s
User 0m0. 020 s
Sys 0m0. 000 s
Instance result analysis: equivalent accumulation calculation, it takes 1.134 seconds to use the shell value calculation, and 0.029 seconds to use the awk tool. The experimental results show that the appropriate use of tool software,
It can significantly improve the execution efficiency of shell scripts.
@ 2: instance: regular expression efficiency.
Requirement: a text is separated by spaces and/to obtain the content of the Fifth Column.
Method 1: use regular [/] to match two separators respectively to obtain the Fifth Column
# Time awk-F' [/] ''{print $5} 'a.txt>/dev/null
Real 0m17. 717 s
User 0m14. 749 s
Sys 0m2. 844 s
Method 2: in actual data analysis, it is found that the single-Character Parsing method can be used. First, 4th columns can be obtained based on the "space" separator, and the second column can be obtained using the "/" separator.
# Time awk '{print $4}' a.txt | awk-F/'{print $2}'>/dev/null
Real 0m0. 565 s
User 0m0. 224 s
Sys 0m0. 688 s
Instance result analysis: awk uses-F to specify the delimiter. When multiple delimiters are used, regular expressions are enabled to parse records, which increases the consumption of function calls and string matching, and improves the efficiency.
Of course, it is not comparable to the simple character comparison method used by default space or single-character separator. However, method 2 relies on further analysis of requirements, and the applicable scope is not
Regular Expressions are simple and flexible.
@ 3: instance: sed application, read the specified row
Method 1: sed-n'45, 50p' filename
Method 2: sed-n' 51q; 45, 50p' filename
Instance result analysis: method 2 adds a judgment based on method 1, and immediately exits when the file is read to 51st rows.Avoid file traversal.Can handle large data volumes
Greatly improve the execution efficiency.
@ 4: Example: sed application, text replacement
Method 1: SED's/Foo/BAR/G' filename
Method 2: sed '/Foo/S/Foo/BAR/G' filename
Instance result analysis: sed supports regular expression matching and replacement. When you want to replace strings, add the address to improve the speed. By adding a judgment logic to the instance,
Use "events"Replace "with" replace", BecauseSed retains the previous regular expression matching environment and does not produce redundant Regular Expression matching.Therefore, method 2 is higher than method 1.
Efficiency
@ 5: The Special Data Structure of awk: A hash array, also known as an associated array. Each unit in the hash array has two elements: the key and the value ).Hash structure
BenefitsIt is reflected in a small memory spaceImplementationBig Data Storage spaceSaves data storage space. At the same time, data access in the hash array is random access and does not need
The array unit value is directly accessed through the hash function, saving the Data Query time.
Reference:
Shell script efficiency: http://blog.csdn.net/yanook/article/details/8395482
Efficiency in Shell