Disclaimer: This article is a blogger hanchao5272 original articles, please indicate the source and leave the original link address, thank you! https://blog.csdn.net/hanchao5272/article/details/89205819
Series catalog References Portal: shell introductory study notes - Prologue
awk built-in functions
function | description |
---|---|
int(expr) |
Truncated to an integer |
sqrt(expr) |
Square root |
rand() |
Returns a random number N , 0 and 1 range,0<N<1 |
srand([expr]) |
Expr used to generate random numbers, if not specified, the default seed current time, if the previous random number seed is used to generate |
asort(a,b) |
A value of the array is sorted, the sorted values saved to the new array b , a new array subscripts sorted from 1 start |
asorti(a,b) |
A subscript of the array is sorted, supra |
sub(r,s[,t]) |
By recording the input s Alternatively r , t an optional replacement for a field, but only the replacement of a string |
gsub(r,s[,t]) |
By recording the input s Alternatively r , t an optional replacement for a field, replaces all |
match(str, regex) |
Test string str contains the matching regex string |
index(str,sub) |
Returns the string str index sub is, 0 that there is no |
length(str) length(arr) |
Returns the length of str; Returns the size of the array arr |
split(str, arr,regex) |
The separator regex will be str divided into an arrayarr |
substr(s,i[,n]) |
String taken s from i the start to the length n , if n not specified, the remaining portion |
tolower(str) |
str All uppercase converted to lowercase |
toupper(str) |
str All lowercase to uppercase |
systime() |
The current time in seconds |
mktime() |
Converting the formatted time to time in seconds |
strftime([format[,timestamp[,utc-flag]]]) |
Formatted output time, a time stamp to a string |
Truncate int (var)
admindeMacBook-Pro:myshell admin$ echo "111 11.1 11a 11a1" |awk '{for(i=1;i<=NF;i++) print $i}'
111
11.1
11a
11a1
admindeMacBook-Pro:myshell admin$ echo "111 11.1 11a 11a1" |awk '{for(i=1;i<=NF;i++) print int($i)}'
111
11
11
11
admindeMacBook-Pro:myshell admin$ echo "111 11.1 11a 11a1" |awk '{for(i=1;i<=NF;i++) print +$i}'
111
11.1
11
11
int(var)
Can interceptvar
the first appeared in整数
int(var)
It is an integer of interception, and this should be+var
distinguished.
Square root sqrt (expr)
admindeMacBook-Pro:myshell admin$ awk 'BEGIN{print sqrt(9)}'
3
A random number rand () and srand ()
rand()
Generated value is fixed, in the range: (0)
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print rand()};'
0.840188
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print rand()};'
0.840188
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print rand();print rand()}'
0.840188
0.394383
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print rand()};'
0.840188
rand()
Need to srand()
combine in order to make changes in the value of sending generated, can be srand()
thought of as a random seed method has changed:
admindeMacBook-Pro:~ admin$ awk 'BEGIN{srand();print rand()};'
0.424539
admindeMacBook-Pro:~ admin$ awk 'BEGIN{srand();print rand()};'
0.780827
By rand()
generating a 1~100
less random numbers:
admindeMacBook-Pro:~ admin$ awk 'BEGIN{srand();print int(rand()*100)};'
63
admindeMacBook-Pro:~ admin$ awk 'BEGIN{srand();print int(rand()*100)};'
49
Sort Sort standard array values asort (a, b) and a lower array asorti (a, b)
数组值排序asort(a,b)和数组下标排序asorti(a,b)
The values are the array after the array is sorted into a b.
# 原始数组
admindeMacBook-Pro:~ admin$ echo 0 |awk 'BEGIN{a[1]="line11";a[3]="line09";a[4]="line12";a[8]="line10"}{for(v in a) print "a["v"]="a[v]}'
a[4]=line12
a[8]=line10
a[1]=line11
a[3]=line09
# asort按照值排序
admindeMacBook-Pro:~ admin$ echo 0 |awk 'BEGIN{a[1]="line11";a[3]="line09";a[4]="line12";a[8]="line10"}{N=asort(a,b);for(j=1;j<=N;j++) print "b["j"]="b[j]}'
b[1]=line09
b[2]=line10
b[3]=line11
b[4]=line12
# asorti按照下标排序
admindeMacBook-Pro:~ admin$ echo 0 |awk 'BEGIN{a[1]="line11";a[3]="line09";a[4]="line12";a[8]="line10"}{N=asorti(a,b);for(j=1;j<=N;j++) print "b["j"]="b[j]}'
b[1]=1
b[2]=3
b[3]=4
b[4]=8
asort
To值
sort the results into an arrayb
, the array index to abandon the original.asorti
To下标
sort the results into an arrayb
, the array abandon the original value.
Alternatively sub field (r, s [, t]) and a global field replacement gsub (r, s [, t])
admindeMacBook-Pro:~ admin$ printf "1 2 3 2 5\n2 5 3 2 1\n1 3 2 1 2\n"
1 2 3 2 5
2 5 3 2 1
1 3 2 1 2
# 替换首个匹配的字段 且 不指定字段
admindeMacBook-Pro:~ admin$ printf "1 2 3 2 5\n2 5 3 2 1\n1 3 2 1 2\n" |awk '{sub(2,0);print $0}'
1 0 3 2 5
0 5 3 2 1
1 3 0 1 2
# 替换全部匹配的字段 且 不指定字段
admindeMacBook-Pro:~ admin$ printf "1 2 3 2 5\n2 5 3 2 1\n1 3 2 1 2\n" |awk '{gsub(2,0);print $0}'
1 0 3 0 5
0 5 3 0 1
1 3 0 1 0
# 替换首个匹配的字段 且 指定字段
admindeMacBook-Pro:~ admin$ printf "1 2 3 2 5\n2 5 3 2 1\n1 3 2 1 2\n" |awk '{sub(2,0,$2);print $0}'
1 0 3 2 5
2 5 3 2 1
1 3 2 1 2
# 替换全部匹配的字段 且 指定字段
admindeMacBook-Pro:~ admin$ printf "1 2 3 2 5\n2 5 3 2 1\n1 3 2 1 2\n" |awk '{gsub(2,0,$2);print $0}'
1 0 3 2 5
2 5 3 2 1
1 3 2 1 2
sub(r,s)
Each row replace only the first matching field.gsub(r,s)
Replacing all the matching record field of each line.sub(r,s)
Thesub(r,s,t)
difference is that the latter only try to match the specified field.
Regular match match (str, regex)
# 存在
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print match("abc123","123")}'
4
# 不存在
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print match("abc123","321")}'
0
String index index (str, sub)
# 单个字符的索引
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print index("hello","h")}'
1
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print index("hello","e")}'
2
# 连续字符的索引
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print index("hello","he")}'
1
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print index("hello","el")}'
2
# 首个匹配的字符
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print index("hello","l")}'
3
# 不存在时,结果为0
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print index("hello","k")}'
0
- From the start index position 1, 0 indicates absence.
- If multiple matches exist, the result shows only the first matching index.
Length of string length ([s])
admindeMacBook-Pro:~ admin$ awk 'BEGIN{a="1111";print length(a)}'
4
admindeMacBook-Pro:~ admin$ awk 'BEGIN{a[1]="1111";a[3]="2222";print length(a)}'
2
length(var)
: Length of varlength(arr)
: The size of the array arr
Split split (str, arr, regex)
admindeMacBook-Pro:~ admin$ awk 'BEGIN{str="10.20.30.40";split(str,arr,".");for(v in arr) print arr[v]}'
40
10
20
30
admindeMacBook-Pro:~ admin$ awk 'BEGIN{str="10.20.30.40";split(str,arr);for(v in arr) print arr[v]}'
10.20.30.40
admindeMacBook-Pro:~ admin$ awk 'BEGIN{str="10 20 30 40";split(str,arr);for(v in arr) print arr[v]}'
40
10
20
30
admindeMacBook-Pro:~ admin$ awk 'BEGIN{FS=".";str="10.20.30.40";split(str,arr);for(v in arr) print arr[v]}'
40
10
20
30
- If no delimiter
regex
is used as a delimiter FS.
Interception string substr (s, i [, n])
admindeMacBook-Pro:~ admin$ awk 'BEGIN{str="123haha";print substr(str,3)}'
3haha
admindeMacBook-Pro:~ admin$ awk 'BEGIN{str="123haha";print substr(str,3,2)}'
3h
substr(s,i)
: Intercepting string s, starting from all i, until the end.substr(s,i,n)
: Intercepting string s, starting from all i, intercept length is n.
Case conversion tolower (s) and toupper (s)
admindeMacBook-Pro:~ admin$ awk 'BEGIN{s="Hello World";print s;print tolower(s);print toupper(s)}'
Hello World
hello world
HELLO WORLD
System time systime, mktime and strftime
By systime
acquiring the system in seconds:
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print systime()}'
1551088337
By systime("date")
acquiring system date:
admindeMacBook-Pro:~ admin$ awk 'BEGIN{system("date");}'
2019年 02月 25日 星期一 17:55:39 CST
By mktime(dataspec)
the "YYYY MM DD HH MM SS"
the time in seconds date format conversion:
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print systime()}'
1551088808
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print mktime("2019 02 25 17 57 30")}'
1551088650
By strftime([format [, timestamp[, utc-flag]]])
time seconds output format:
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print strftime("%Y-%m-%d %H:%M:%S",systime())}'
2019-02-25 18:03:00
admindeMacBook-Pro:~ admin$ awk 'BEGIN{print strftime("%Y-%m-%d %H:%M:%S",mktime("2019 02 25 17 57 30"))}'
2019-02-25 17:57:30
Here is the strftime
formatting characters can be used:
SN | description |
---|---|
%a | The acronym week (Mon-Sun). |
%A | Week full name (Monday-Sunday). |
%b | Month abbreviation (Jan). |
%B | The full name of the month (January). |
%c | Local date and time. |
%C | Century portion of a year in which the year is divisible by 100. |
%d | Decimal date (01-31) |
%D | Equivalent to% m /% d /% y. |
%e | Date, if there is only one digit is padded with spaces |
%F | Equivalent% Y-% m-% d, which is the ISO 8601 standard date format. |
%g | Year ISO8610 standard peripheral mold 100 where the addition (00-99). For example, January 1, 1993 belongs to the first 53 weeks of 1992. So, while it was in 1993, Day 1, but its ISO8601 standard year where the week is 1992. Similarly, although the December 31, 1973 belonging to 1973 but it belongs to the first week of 1994. So the standard of ISO8610 Week December 31, 1973 where is 1974 instead of 1973. |
%G | The full name of the year where the ISO standard week. |
%h | Equivalent% b. |
%H | Hours 24 hours format with decimal representation (00-23) |
%I | Hour with the 12-hour format decimal representation (00-12) |
%j | The first few days (001-366) of the year |
%m | Month (01-12) |
%M | The number of minutes (00-59) |
%n | Newline (ASCII LF) |
%p | Duodecimal notation (AM / PM) |
%r | Method duodecimal represents time (equivalent to% I:% M:% S% p). |
%R | It is equivalent to% H:% M. |
%S | Second time value (00-60) |
%t | Tabs (tab) |
%T | It is equivalent to% H:% M:% S. |
in% | Week (1-7) digital representation, 1 for Monday. |
% U | The first few weeks of the year (the first Sunday as the start of the first week), 00-53 |
% V | The first few weeks of the year (the first Monday as the first week of the start), 01-53. |
%w | Week (0-6) to a digital representation, 0 represents Sunday. |
%W | Decimal representation of the first few weeks (the first Monday as the first week of the beginning) of the year, 00-53. |
%x | Local date representation |
%X | Local time |
%Y | Year mod 100. |
%Y | Decimal representation of the full year. |
%with | Time zone, represented in the format + HHMM (e.g., format requirements RFC 1036 or RFC 822 header generation time) |
%WITH | Time zone name or abbreviation, if no time zone pending output. |
awk custom function
grammar
function function_name(param1,param2,...)
{
body
}
Examples
[worker@c2-a02-126-10-4 hanchao]$ cat func.awk
# sum
function sum(a,b)
{
return a + b;
}
# avg
function avg(a,b)
{
return (a + b) / 2;
}
# main
function main(a,b)
{
print "sum=",sum(a,b);
print "avg=",avg(a,b);
}
BEGIN{
main(10,20)
}
[worker@c2-a02-126-10-4 hanchao]$ awk -f func.awk
sum= 30
avg= 15