当前位置:网站首页>Awk from digging into the ground to getting started (10) awk built-in functions
Awk from digging into the ground to getting started (10) awk built-in functions
2022-07-04 08:46:00 【Wonderful binary】
AWK It provides rich built-in functions for program developers . This chapter will explain AWK Arithmetic functions provided 、 String manipulation functions 、 Time operation related functions 、 Bit manipulation functions and various other functions .
1, String function
Function name | describe |
---|---|
sub | The largest... In the matching record 、 Regular expression for the leftmost substring , And replace these strings with replacement strings . If no target string is specified, the entire record is used by default . Replacement only occurs in the first match When |
gsub | Match throughout the document |
index | Returns the position where the substring was first matched , Offset from position 1 Start |
substr | Back from position 1 The starting substring , If the specified length exceeds the actual length , Just return the whole string |
split | The string can be divided into an array according to the given separator . If the separator is not provided , According to the current FS Value to split |
length | Returns the number of characters recorded |
match | Returns the index of the location of the regular expression in a string , If the specified regular expression cannot be found 0.match Function sets the built-in variable RSTART Is the starting bit of the substring of the string Set up ,RLENGTH Is the number of characters to the end of the substring .substr These variables can be used to intercept strings |
toupper and tolower | It can be used for conversion between string sizes , This function is only available in gawk Effective in |
2, Mathematical functions
Function name | Return value |
---|---|
atan2(x,y) | y,x Cotangent of a range |
cos(x) | cosine function |
exp(x) | seek power |
int(x) | integer |
log(x) | Nature is right Count |
rand() | random number |
sin(x) | sine |
sqrt(x) | flat root |
srand(x) | x yes rand() The seed of function |
int(x) | take whole , The process is not rounded |
rand() | Produce a greater than or equal to 0 Less than 1 The random number |
Arithmetic functions
AWK The following built-in arithmetic operation functions are provided :
atan2(y,x)
This function returns the tangent value y/x Angle value of , The angle is in radians . Examples are as follows :
[jerry]$ awk 'BEGIN {
PI = 3.14159265
x = -10
y = 10
result = atan2 (y,x) * 180 / PI;
printf "The arc tangent for (x=%f, y=%f) is %f degrees\n", x, y, result
}'
Execute the above command to get the following results :
The arc tangent for (x=-10.000000, y=10.000000) is 135.000000 degrees
cos(expr)
This function returns expr Cosine of , Input parameters are in radians . Examples are as follows :
[jerry]$ awk 'BEGIN {
PI = 3.14159265
param = 60
result = cos(param * PI / 180.0);
printf "The cosine of %f degrees is %f.\n", param, result
}'
Execute the above command to get the following results :
The cosine of 60.000000 degrees is 0.500000.
exp(expr)
This function returns a natural number e Of expr The next power .
[jerry]$ awk 'BEGIN {
param = 5
result = exp(param);
printf "The exponential value of %f is %f.\n", param, result
}'
Execute the above command to get the following result :
The exponential value of 5.000000 is 148.413159.
int(expr)
This function returns a number expr The integral part of . Examples are as follows :
[jerry]$ awk 'BEGIN {
param = 5.12345
result = int(param)
print "Truncated value =", result
}'
Execute the above command to get the following result :
Truncated value = 5
log(expr)
This function evaluates expr Natural logarithm .
[jerry]$ awk 'BEGIN {
param = 5.5
result = log (param)
printf "log(%f) = %f\n", param, result
}'
Execute the above command to get the following result :
log(5.500000) = 1.704748
rand
rand Function returns a value greater than or equal to 0 Less than 1 The random number N(0<= N < 1). Examples are as follows :
[jerry]$ awk 'BEGIN {
print "Random num1 =" , rand()
print "Random num2 =" , rand()
print "Random num3 =" , rand()
}'
Execute the above command to get the following result :
Random num1 = 0.237788
Random num2 = 0.291066
Random num3 = 0.845814
sin(expr)
Sine function returns angle expr The sine of , The angle is in radians . Examples are as follows :
[jerry]$ awk 'BEGIN {
PI = 3.14159265
param = 30.0
result = sin(param * PI /180)
printf "The sine of %f degrees is %f.\n", param, result
}'
Execute the above command to get the following result :
The sine of 30.000000 degrees is 0.500000.
sqrt(expr)
This function evaluates expr The square root of .
[jerry]$ awk 'BEGIN {
param = 1024.0
result = sqrt(param)
printf "sqrt(%f) = %f\n", param, result
}'
Execute the above command to get the following result :
sqrt(1024.000000) = 32.000000
srand([expr])
This function uses seed values to generate random numbers , The number expr As seed value of random number generator . If not specified expr By default, the function uses the current system time as the seed value .
[jerry]$ awk 'BEGIN {
param = 10
printf "srand() = %d\n", srand()
printf "srand(%d) = %d\n", param, srand(param)
}'
Execute the above command to get the following results :
srand() = 1
srand(10) = 1417959587
String function
AWK The string operation functions shown below are provided :
asort(arr,[, d [,how] ])
asort Function USES GAWK General rules for value comparison arr The content in , And then to 1 Replace the index of the sorted content with the starting ordered integer .
[jerry]$ awk 'BEGIN {
arr[0] = "Three"
arr[1] = "One"
arr[2] = "Two"
print "Array elements before sorting:"
for (i in arr) {
print arr[i]
}
asort(arr)
print "Array elements after sorting:"
for (i in arr) {
print arr[i]
}
}'
Execute the above command to get the following result :
Array elements before sorting:
Three
One
Two
Array elements after sorting:
One
Three
Two
asorti(arr,[, d [,how] ])
asorti The behavior of a function is related to asort The behavior of functions is very similar , The difference between them is aosrt Sort the values of the array , and asorti Sort the index of the array .
[jerry]$ awk 'BEGIN {
arr["Two"] = 1
arr["One"] = 2
arr["Three"] = 3
asorti(arr)
print "Array indices after sorting:"
for (i in arr) {
print arr[i]
}
}'
Execute the above command to get the following result :
Array indices after sorting:
One
Three
Two
gsub(regexp, replacement [, target])
gsub It's a global replacement ( global substitution ) Abbreviation . It will appear in the substring regexp Replace with replacement . The third parameter target It's optional , The default value is $0, Means to search the substring in the whole input record .
[jerry]$ awk 'BEGIN {
str = "Hello, World"
print "String before replacement = " str
gsub("World", "Jerry", str)
print "String after replacement = " str
}'
Execute the above command to get the following result :
String before replacement = Hello, World
String after replacement = Hello, Jerry
index(str,sub)
index The function is used to detect a string sub Whether it is str The string of . If sub yes str The string of , Then return the substring sub In string str The beginning of ; If not for its substring , Then return to 0.str The character position index of is from 1 Start counting .
[jerry]$ awk 'BEGIN {
str = "One Two Three"
subs = "Two"
ret = index(str, subs)
printf "Substring \"%s\" found at %d location.\n", subs, ret
}'
Execute the above command to get the following result :
Substring "Two" found at 5 location.
length(str)
length Function returns the length of the string .
[jerry]$ awk 'BEGIN {
str = "Hello, World !!!"
print "Length = ", length(str)
}'
Execute the above command to get the following result :
Length = 16
match(str, regex)
Search for regular expressions in strings regexp Longest match 、 Leftmost substring , And return the character position at the beginning of the substring ( Indexes ). If the match fails, it returns 0.
[jerry]$ awk 'BEGIN {
str = "One Two Three"
subs = "Two"
ret = match(str, subs)
printf "Substring \"%s\" found at %d location.\n", subs, ret
}'
Execute the above command to get the following result :
Substring "Two" found at 5 location.
split(str, arr,regex)
split Functions use regular expressions regex Split string str. All the results after segmentation are stored in the array arr in . If not specified regex Then use FS segmentation .
[jerry]$ awk 'BEGIN {
str = "One,Two,Three,Four"
split(str, arr, ",")
print "Array contains following values"
for (i in arr) {
print arr[i]
}
}'
Execute the above command to get the following result :
Array contains following values
One
Two
Three
Four
sprintf(format,expr-list)
sprintf Function in the specified format ( format ) List the parameters expr-list Construct into a string and return .
[jerry]$ awk 'BEGIN {
str = sprintf("%s", "Hello, World !!!")
print str
}'
Execute the above command to get the following result :
Hello, World !!!
strtonum(str)
strtonum The string str Convert to numeric . If the string 0 Start , Then treat it as an octal number ; If the string 0x or 0X Start , Then treat it as a hexadecimal number ; otherwise , Treat it as a floating point number .
[jerry]$ awk 'BEGIN {
print "Decimal num = " strtonum("123")
print "Octal num = " strtonum("0123")
print "Hexadecimal num = " strtonum("0x123")
}'
Execute the above command to get the following result :
Decimal num = 123
Octal num = 83
Hexadecimal num = 291
sub(regex,sub,string)
sub Function performs a substring replacement . It will appear for the first time with regex The matching substring is used sub Replace . The third parameter is optional , The default is $0.
[jerry]$ awk 'BEGIN {
str = "Hello, World"
print "String before replacement = " str
sub("World", "Jerry", str)
print "String after replacement = " str
}'
Execute the above command to get the following result :
String before replacement = Hello, World
String after replacement = Hello, Jerry
substr(str, start, l)
substr The function returns str In a string, from start Characters start with a length of l The string of . If not specified l Value , return str From start Suffix substring starting with characters .
[jerry]$ awk 'BEGIN {
str = "Hello, World !!!"
subs = substr(str, 1, 5)
print "Substring = " subs
}'
Execute the above command to get the following result :
Substring = Hello
tolower(str)
This function will string str Convert all uppercase letters to lowercase letters and return . Be careful , character string str Itself is not changed .
[jerry]$ awk 'BEGIN {
str = "HELLO, WORLD !!!"
print "Lowercase string = " tolower(str)
}'
Execute the above command to get the following result :
Lowercase string = hello, world !!!
toupper(str)
This function will string str Convert all lowercase letters into uppercase letters and return . Be careful , character string str Itself is not changed .
[jerry]$ awk 'BEGIN {
str = "hello, world !!!"
print "Uppercase string = " toupper(str)
}'
Executing the above command can get the following results :
Uppercase string = HELLO, WORLD !!!
Time function
AWK The following built-in time functions are provided :
systime
This function returns from Epoch In seconds to the current time ( stay POSIX On the system ,Epoch by 1970-01-01 00:00:00 UTC).
[jerry]$ awk 'BEGIN {
print "Number of seconds since the Epoch = " systime()
}'
Execute the above command to get the following result :
Number of seconds since the Epoch = 1418574432
mktime(dataspec)
This function will string dataspec Convert to and from systime Return a timestamp with similar values . dataspec The format of the string is YYYY MM DD HH MM SS.
[jerry]$ awk 'BEGIN {
print "Number of seconds since the Epoch = " mktime("2014 12 14 30 20 10")
}'
Execute the above command to get the following result :
Number of seconds since the Epoch = 1418604610
strftime([format [, timestamp[, utc-flag]]])
This function is based on format The specified format will timestamp timestamp format .
[jerry]$ awk 'BEGIN {
print strftime("Time = %m/%d/%Y %H:%M:%S", systime())
}'
Executing the above command can get the following results :
Time = 12/14/2014 22:08:42
Here is AWK Description of different date formats supported :
SN | describe |
---|---|
%a | Week abbreviation (Mon-Sun). |
%A | The full name of the week (Monday-Sunday). |
%b | Month abbreviation (Jan). |
%B | Full name of the month (January). |
%c | Local date and time . |
%C | The century part of the year , The value is divided by the year 100. |
%d | Decimal date (01-31) |
%D | Equivalent to %m/%d/%y. |
%e | date , If there is only one digit, fill in the blanks |
%F | Equivalent to %Y-%m-%d, This is also ISO 8601 Standard date format . |
%g | ISO8610 The year of the standard week is divided by 100(00-99). such as ,1993 year 1 month 1 Japan belongs to 1992 In the first 53 Zhou . therefore , Although it is 1993 In the first 1 God , But its ISO8601 The year of standard week is 1992. Again , Even though 1973 year 12 month 31 Japan belongs to 1973 But it belongs to 1994 The first week of the year . therefore 1973 year 12 month 31 Japanese ISO8610 The year of standard week is 1974 instead of 1973. |
%G | ISO The full name of the year of the standard week . |
%h | Equivalent to %b. |
%H | Expressed in decimal system 24 Hours in hours format (00-23) |
%I | Expressed in decimal system 12 Hours in hours format (00-12) |
%j | The day of the year (001-366) |
%m | month (01-12) |
%M | Minutes (00-59) |
%n | A newline (ASCII LF) |
%p | The decimal notation (AM/PM) |
%r | The time of the decimal notation ( Equivalent to %I:%M:%S %p). |
%R | Equivalent to %H:%M. |
%S | The second value of time (00-60) |
%t | tabs (tab) |
%T | Equivalent to %H:%M:%S. |
%u | A week in numbers (1-7),1 For Monday . |
%U | The week of the year ( The first Sunday is the beginning of the first week ),00-53 |
%V | The week of the year ( The first Monday is the beginning of the first week ),01-53. |
%w | A week in numbers (0-6),0 Means Sunday . |
%W | The week of the year in decimal notation ( The first Monday is the beginning of the first week ),00-53. |
%x | Local date means |
%X | Local time means |
%y | Year Division 100. |
%Y | The complete year in decimal . |
%z | The time zone , The format is +HHMM( for example , The format requires the generation of RFC 822 perhaps RFC 1036 Time head ) |
%Z | Time zone name or abbreviation , If the time zone is undetermined, there is no output . |
Bit operation function
AWK The following built-in bit operation functions are provided :
and
Perform bit and operation .
[jerry]$ awk 'BEGIN {
num1 = 10
num2 = 6
printf "(%d AND %d) = %d\n", num1, num2, and(num1, num2)
}'
Execute the above command to get the following result :
(10 AND 6) = 2
compl
Complement by position .
[jerry]$ awk 'BEGIN {
num1 = 10
printf "compl(%d) = %d\n", num1, compl(num1)
}'
Execute the above command to get the following result :
compl(10) = 9007199254740981
lshift
Left shift operation .
[jerry]$ awk 'BEGIN {
num1 = 10
printf "lshift(%d) by 1 = %d\n", num1, lshift(num1, 1)
}'
Execute the above command to get the following result :
lshift(10) by 1 = 20
rshift
Shift right .
[jerry]$ awk 'BEGIN {
num1 = 10
printf "rshift(%d) by 1 = %d\n", num1, rshift(num1, 1)
}'
Execute the above command to get the following result :
rshift(10) by 1 = 5
or
To press or operate .
[jerry]$ awk 'BEGIN {
num1 = 10
num2 = 6
printf "(%d OR %d) = %d\n", num1, num2, or(num1, num2)
}'
Execute the above command to get the following result :
(10 OR 6) = 14
xor
Operate by bitwise exclusive or .
[jerry]$ awk 'BEGIN {
num1 = 10
num2 = 6
printf "(%d XOR %d) = %d\n", num1, num2, xor(num1, num2)
}'
Execute the above command to get the following result :
(10 bitwise xor 6) = 12
Other functions
Other functions mainly include :
close(expr)
Close the file of the pipeline .
[jerry]$ awk 'BEGIN {
cmd = "tr [a-z] [A-Z]"
print "hello, world !!!" |& cmd
close(cmd, "to")
cmd |& getline out
print out;
close(cmd);
}'
Execute the above command to get the following result :
HELLO, WORLD !!!
Does the content of the script look mysterious ? Let's uncover its mystery .
- The first sentence cmd = “tr [a-z] [A-Z]” stay AWK A two-way communication channel is established in .
- The second sentence print by tr Command provides input .&| Indicates two-way communication .
- The third sentence close(cmd, “to”) Close after execution to process .
- The fourth sentence cmd |& getline out Use getline Function stores the output to out variable .
- The next output statement prints the contents of the output , Last close Function off cmd.
delete
delete Used to delete elements from an array . The following example shows how to use delete:
[jerry]$ awk 'BEGIN {
arr[0] = "One"
arr[1] = "Two"
arr[2] = "Three"
arr[3] = "Four"
print "Array elements before delete operation:"
for (i in arr) {
print arr[i]
}
delete arr[0]
delete arr[1]
print "Array elements after delete operation:"
for (i in arr) {
print arr[i]
}
}'
Execute the above command to get the following result :
Array elements before delete operation:
One
Two
Three
Four
Array elements after delete operation:
Three
Four
exit
This function terminates script execution . It can accept optional parameters expr Pass on AWK Return to status . Examples are as follows :
[jerry]$ awk 'BEGIN {
print "Hello, World !!!"
exit 10
print "AWK never executes this statement."
}'
Execute the above command to get the following result :
Hello, World !!!
flush
flush Function to flush the buffer of an open file or pipeline . How to use it is as follows :
fflush([output-expr])
If not provided output-expr,fflush Standard output will be refreshed . if output-epxr Is an empty string (“”),fflush All open files and pipelines will be refreshed .
getline
getline Function reads into the next line . Use... In the example getline From file marks.txt Read in a line and output :
[jerry]$ awk '{getline; print $0}' marks.txt
Execute the above command to get the following result :
2) Rahul Maths 90
4) Kedar English 85
5) Hari History 89
The script seems to work properly , But where did the first line go ? Let's sort out the whole process . When it's just started ,AWK From file marks.txt Read in a row and store it in the variable $0 in . In the next statement , We use getline Read in the next line . therefore AWK Read in the second line and store it in $0 in . Last ,AWK Use print Output the contents of the second line . This process continues until the end of the file .
next
next Stop processing the current record , And enter the processing process of the next record . In the following example , When the pattern string matching is successful, the program does not perform any operation :
[jerry]$ awk '{if ($0 ~/Shyam/) next; print $0}' marks.txt
Execute the above command to get the following result :
1) Amit Physics 80
2) Rahul Maths 90
4) Kedar English 85
5) Hari History 89
nextfile
nextfile Stop processing the current file , Start processing from the first record of the next file . In the following example , When the match is successful, stop processing the first file and process the second file :
First create two files . file1.txt The contents are as follows :
file1:str1
file1:str2
file1:str3
file1:str4
file file2.txt The contents are as follows :
file2:str1
file2:str2
file2:str3
file2:str4
Now let's test nextfile function .
[jerry]$ awk '{ if ($0 ~ /file1:str2/) nextfile; print $0 }' file1.txt file2.txt
Execute the above command to get the following result :
file1:str1
file2:str1
file2:str2
file2:str3
file2:str4
return
return Used to return values from user-defined functions . Please note that , If no return value is specified , Then the return value of is undefined . The following example demonstrates return How to use :
First , create a file functions.awk, The contents are as follows :
function addition(num1, num2)
{
result = num1 + num2
return result
}
BEGIN {
res = addition(10, 20)
print "10 + 20 = " res
}
Execute the above command to get the following result :
10 + 20 = 30
system
system Function can execute a specific command and then return its exit state . The return value is 0 Indicates that the command executed successfully ; Not 0 Indicates that the command execution failed . In the following example Date Display the current system time , Then output the return status of the command :
[jerry]$ awk 'BEGIN { ret = system("date"); print "Return value = " ret }'
Execute the above command to get the following result :
Sun Dec 21 23:16:07 IST 2014
Return value = 0
边栏推荐
- Educational Codeforces Round 119 (Rated for Div. 2)
- User login function: simple but difficult
- Flutter integrated amap_ flutter_ location
- Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
- Basic discipline formula and unit conversion
- SSRF vulnerability exploitation - attack redis
- awk从入门到入土(14)awk输出重定向
- 一文了解数据异常值检测方法
- [attack and defense world | WP] cat
- Relationship and operation of random events
猜你喜欢
How to solve the problem of computer jam and slow down
ES6 summary
OpenFeign 服务接口调用
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
DM database password policy and login restriction settings
How can we make a monthly income of more than 10000? We media people with low income come and have a look
ArcGIS application (XXII) ArcMap loading lidar Las format data
地平线 旭日X3 PI (一)首次开机细节
09 softmax regression + loss function
How to choose solid state hard disk and mechanical hard disk in computer
随机推荐
如何通过antd的upload控件,将图片以文件流的形式发送给服务器
团体程序设计天梯赛-练习集 L1-006 连续因子
What if I forget the router password
What if the wireless network connection of the laptop is unavailable
Comparison between sentinel and hystrix
How college students choose suitable computers
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
HMS core helps baby bus show high-quality children's digital content to global developers
awk从入土到入门(10)awk内置函数
es6总结
FOC控制
User login function: simple but difficult
PHP session variable passed from form - PHP
Live in a dream, only do things you don't say
SSRF vulnerability exploitation - attack redis
The upper layer route cannot Ping the lower layer route
[attack and defense world | WP] cat
Awk from entry to earth (12) awk can also write scripts to replace the shell
Use preg_ Match extracts the string into the array between: & | people PHP
Flutter 集成 amap_flutter_location